HA – setting automations/events/color/conditions, without fiddling around in HA settings


My wife likes to set/change colors/brightness of our lights, based on certain conditions/events, but
she doesn’t want to fiddle around in the HA interface or yaml itself. I created some buttons on dashboards, so she can set the colors/brightness of every light,
and can even change the conditions and events herself. And this only with a few extra buttons. She can even store these “settings”, and view them on a custom dynamic HTML page.
You will need PyScript and MySQL installed onto Home Assistant, and have access to the folder where you can put HTML files.
Furthermore, you will need to create some helpers, to create custom triggers.

First, an overview/explanation, on how this system works:

Every device exists of 7 components:

1. Big element:
– This element does have a bright color when light is on, and a dark color when light is off.
– By clicking on this element, you can manually turn the light on/off.
– a Long press (until a circle appears), gives access to the brightness- and color-palette.
– Double click, gives the possibility to delete the scene definition for this device.
– The name of the device does have a <| or |> symbol at the beginning or end.
– Symbol on the left, is for automation A. Symbol on the right, is for automation B.
|> indicates all light-levels from high until the current level, will be run through.
<| indicated all light-levels from low until the current level, will be run through.

2. The 6 smaller elements:
– the 6 smaller elements, consists of 2 rows of each 3 buttons. First 3 buttons are for automation A, second row for automation B.
– From left to right, the buttons are: Action – When – Performance , the combination of all 3 buttons, form together one automation.
– By clicking on a button again and again, you get the next possibility for this button. When at the end of possibilities, you get back to the first one.
– A long press on a button, you get the question if you want to save the current color/brightness, for the current button-combination. This can be done for A and B separately.
– A double-click on the “when” button, adjusts the curve for light-brightness ( <| or |> )
– A double-click on the “Action” or “Performance” button, give you an overview of the saved automations for the current device. (See next screenshot)


3. The “Action” button:

IconDescription
“Hand”, this is used for manual control, no automation is executed.
“Gear”, used for “simple” automation. (mostly used with (simple) motion sensors)
“Bed”, only executed when everybody is in bed.
“Motion”, executed when motion is detected, when everybody is out of bed.
“Person”, executed when at least one person OUT and one person IN bed.
“Sun on horizon”, executed in combination curve of light-brightness, independent of motion.
“Security shield”, executed when alarm is activated.


4. The “When” button:

IconDescription
Execute always, day and night.
Trigger only with bright weather.
Trigger with “regular” weather, not super-bright but also not gloomy weather.
Trigger with half-dark weather but with enough brightness, mostly used with sunrise or sunset.
More dark, period past sunrise or sunset.
Ideal to trigger external lighting, already quite dark
Pitch dark


5. The “Performance” button:

IconDescription
Turn on when motion/presence detected, turn off when motion/presence is no longer detected.
Turn on, when motion/presence is detected.
Turn off, when motion/presence is detected.
Turn on, when motion/presence is no longer detected.
Turn off, when motion/presence is no longer detected.


6. A few examples:

#ActionWhenPerformanceCurveDescription
A|>Automation is performed when everybody is in bed,
from the moment it’s more dark. (just before sunrise), from pitch dark until “more dark”. When you for example want to turn on a device when there is movement/presence. When no movement/presence is detected, the device will turn off again.
B|>Automation is performed, when at least one person is in bed, from the moment it’s more dark. (just before sunrise), from pitch dark until “more dark”. When you for example want to turn on a device when there is movement/presence. When no movement/presence is detected, the device will turn off again.
A|>Automation is performed when a change of light-level occurs. Device turns on when it’s quite dark, and turns off again, when there is more light. (Automation is “on” between “quite dark” and “pitch dark”.)
A<|Automation is triggered at high light level conditions, when motion/presence is detected, and turns off when no motion/presence is detected. (Runs only from high light level and above)
AAutomation is triggered when motion/presence is detected, when alarm is activated, during every light-level, and will not turn off again automatically.


7. How to do it – preparation & installation:
* First, Install PyScript via HACS. https://github.com/custom-components/pyscript
* Also install Browser Mod via HACS. https://github.com/thomasloven/hass-browser_mod
* Next, make sure you are running MariaDB/MySQL, and install PHPMyAdmin. https://community.home-assistant.io/t/home-assistant-community-add-on-phpmyadmin/171729
Use the following script, to create a database and necessary tables on your MySQL instance, via PHPMyAdmin. Change the Database-name as needed !
If everything turns out fine, you should have a database with a table SceneDefinitions, with 3 test-records in it.

-- phpMyAdmin SQL Dump

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
START TRANSACTION;
SET time_zone = "+00:00";

CREATE DATABASE IF NOT EXISTS `homeassistant` DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
USE `homeassistant`;

CREATE TABLE `SceneDefinitions` (
  `DefName` varchar(50) NOT NULL,
  `A_or_B` varchar(1) NOT NULL,
  `ActionNr` mediumint(9) NOT NULL,
  `WhenNr` mediumint(9) NOT NULL,
  `DoNr` mediumint(9) NOT NULL,
  `R` mediumint(9) NOT NULL,
  `G` mediumint(9) NOT NULL,
  `B` mediumint(9) NOT NULL,
  `Brightness` mediumint(9) NOT NULL,
  `Color_Temp` mediumint(9) NOT NULL,
  `CreationDate` datetime NOT NULL,
  `ModificationDate` datetime NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

INSERT INTO `SceneDefinitions` (`DefName`, `A_or_B`, `ActionNr`, `WhenNr`, `DoNr`, `R`, `G`, `B`, `Brightness`, `Color_Temp`, `CreationDate`, `ModificationDate`) VALUES
('status_berging_plafond', 'A', 2, 1, 1, 0, 0, 0, 255, 4016, '2022-11-12 15:18:23', '2022-11-12 15:18:23'),
('status_berging_plafond', 'B', 1, 1, 1, 0, 0, 0, 255, 4098, '2022-11-12 15:57:45', '2022-11-12 17:45:55');
COMMIT;

* After this, download the following 4 PyScripts, and store the in your config/pyscript folder.
You should edit those files, to reflect the correct host-IP / username / password / database name.
Furthermore, there are some specifics into this code (mainly the scenedefinition_automation.py, the heart of the system)
Feel free to take the out, or modify this script to your needs …






*One of the functions, is showing info about the defined SceneDefinitions, this is done via an SQL command, to retrieve the data from the MySQL database, and show it in an HTML page.
You first need to download the following zip-file, and extract it in a config/www/scenedefinitions folder.
Of course, you can modify the HTML layout to your own preferences 🙂



The info-pyscript uses 2 shell commands, you need to define them within your configuration.yaml, under a shell_command section.
More info about the shell command within HA: https://www.home-assistant.io/integrations/shell_command/

delete_scenedefinitions: ssh -o 'StrictHostKeyChecking=no' -o UserKnownHostsFile=/dev/null -i /config/ssh/id_rsa root@HA_IPADDRESS "docker exec addon_core_mariadb rm -rf /DATAEXPORT.CSV"
copy_scenedefinitions: ssh -o 'StrictHostKeyChecking=no' -o UserKnownHostsFile=/dev/null -i /config/ssh/id_rsa root@HA_IPADDRESS "docker cp addon_core_mariadb:/DATAEXPORT.CSV /config/www/scenedefinitions/"

The first one, is to delete the data-export of the SQL result, from within the MariaDB docker container.
It can’t be over-written, so a delete before doing an export, is the only solution.
The second one, is to copy the actual CSV file with the SQL result, from the MariaDB docker container, to the actual HA instance, so that it can be used from within the HTML part, to show the result …
Of course this only works, when you replace HA_IPADDRESS with the actual IP address with your HA IP address, and that SSH is configured correctly, so that it can be used on itself,
without the need of typing the root password. See this article for more info and a how-to: https://www.digitalplayground.be/?p=6052

We are almost there 🙂
Just 2 small things to do:
1. Create an input_text helper with the following name: input_text.helper_scenedefinitions (min-length=0 / max-length=100)
2. Create the following automation:

alias: HELPER - scenedefinition_info
description: ""
trigger:
  - platform: state
    entity_id:
      - input_text.helper_scenedefinitions
condition: []
action:
  - service: pyscript.scenedefinition_info
    data: {}
mode: single

8. How to do it – The actual triggers:
Now that all installations and preparations are done, we can use all this “functionality” inside triggers …
We have 6 different buttons (2 sets of each buttons), each button does need his own script.
The first 3 scripts and next 3 scripts are almost identical …
For every device you want to use with this automation, you need to define 3 (or 6) counters:
1. counter.devicename_action
2. counter.devicename_when
3. counter.devicename_do
(all with min=1 / max=10 / begin=1 / step=1)
Furthermore, you need an input_boolean.devicename_dir
Depending if you are going to use a second row of buttons (so, a total of 6 buttons), you need
to define a second set of these 4 variables, ending with and _b
(so, counter.devicename_action becomes counter.devicename_action_b)

Furthermore, we need 3 extra automations, to keep actions/whens/dos, within their range, and reset them if necessary. The Actions and Whens need to stay below 7, and the Dos need to stay below 5.
If you think up of some extra Actions/Whens/Dos, you need to change this automation accordingly.
Simply create an automation per type, where you group ALL your helpers within these automations.
Creating an automation for every helper is also possible, but this will give you a giant list of extra automations, it’s better to group them within an automation itself …

Here is an example for 3 when-helpers. Build this for your Dos and Actions accordingly !

alias: HELPER - when-statussen - reset to 1 if higher then 7
description: ""
trigger:
  - platform: numeric_state
    entity_id: counter.aanrecht_tl_when
    above: 7
  - platform: numeric_state
    entity_id: counter.aanrecht_tl_when_b
    above: 7
  - platform: numeric_state
    entity_id: counter.aanrecht_ledstrip_when
    above: 7
  - platform: numeric_state
    entity_id: counter.aanrecht_ledstrip_when_b
    above: 7
  - platform: numeric_state
    entity_id: counter.badkamer_lampjes_when
    above: 7
  - platform: numeric_state
    entity_id: counter.badkamer_lampjes_when_b
    above: 7
condition: []
action:
  - if:
      - condition: numeric_state
        entity_id: counter.aanrecht_ledstrip_when
        above: 7
    then:
      - service: counter.reset
        data: {}
        target:
          entity_id: counter.aanrecht_ledstrip_when
  - if:
      - condition: numeric_state
        entity_id: counter.aanrecht_ledstrip_when_b
        above: 7
    then:
      - service: counter.reset
        data: {}
        target:
          entity_id: counter.aanrecht_ledstrip_when_b
  - if:
      - condition: numeric_state
        entity_id: counter.aanrecht_tl_when
        above: 7
    then:
      - service: counter.reset
        data: {}
        target:
          entity_id: counter.aanrecht_tl_when
  - if:
      - condition: numeric_state
        entity_id: counter.aanrecht_tl_when_b
        above: 7
    then:
      - service: counter.reset
        data: {}
        target:
          entity_id: counter.aanrecht_tl_when_b
  - if:
      - condition: numeric_state
        entity_id: counter.badkamer_lampjes_when
        above: 7
    then:
      - service: counter.reset
        data: {}
        target:
          entity_id: counter.badkamer_lampjes_when
  - if:
      - condition: numeric_state
        entity_id: counter.badkamer_lampjes_when_b
        above: 7
    then:
      - service: counter.reset
        data: {}
        target:
          entity_id: counter.badkamer_lampjes_when_b
mode: single


Button 1 – Action

show_name: false
show_icon: true
type: custom:button-card
entity: counter.tuin_hoeklamp_action
double_tap_action:
  action: call-service
  service: input_text.set_value
  service_data:
    value: status_tuin_hoeklamp
    entity_id: input_text.helper_scenedefinitions
hold_action:
  confirmation:
    text: |
      [[[
          var TxtQuestion = "Instellingen gebruiken voor status ["
          if (states["counter.tuin_hoeklamp_action"].state == '1') TxtQuestion = TxtQuestion + "manueel/"
          if (states["counter.tuin_hoeklamp_action"].state == '2') TxtQuestion = TxtQuestion + "automatisch/"
          if (states["counter.tuin_hoeklamp_action"].state == '3') TxtQuestion = TxtQuestion + "iedereen in bed/"
          if (states["counter.tuin_hoeklamp_action"].state == '4') TxtQuestion = TxtQuestion + "iedereen uit bed/"
          if (states["counter.tuin_hoeklamp_action"].state == '5') TxtQuestion = TxtQuestion + "sommigen in- & uit bed/"
          if (states["counter.tuin_hoeklamp_action"].state == '6') TxtQuestion = TxtQuestion + "zon op-onder/"
          if (states["counter.tuin_hoeklamp_action"].state == '7') TxtQuestion = TxtQuestion + "alarm/"
          if (states["counter.tuin_hoeklamp_when"].state == '1') TxtQuestion = TxtQuestion + "altijd/"
          if (states["counter.tuin_hoeklamp_when"].state == '2') TxtQuestion = TxtQuestion + "dag - vrij helder/"
          if (states["counter.tuin_hoeklamp_when"].state == '3') TxtQuestion = TxtQuestion + "dag - normaal/"
          if (states["counter.tuin_hoeklamp_when"].state == '4') TxtQuestion = TxtQuestion + "dag - overtrokken/"
          if (states["counter.tuin_hoeklamp_when"].state == '5') TxtQuestion = TxtQuestion + "donker - vrij helder/"
          if (states["counter.tuin_hoeklamp_when"].state == '6') TxtQuestion = TxtQuestion + "donker - normaal/"
          if (states["counter.tuin_hoeklamp_when"].state == '7') TxtQuestion = TxtQuestion + "donker - meest donker/"
          if (states["counter.tuin_hoeklamp_do"].state == '1') TxtQuestion = TxtQuestion + "alle bewegingen] ?"
          if (states["counter.tuin_hoeklamp_do"].state == '2') TxtQuestion = TxtQuestion + " aan bij voorwaarden] ?"
          if (states["counter.tuin_hoeklamp_do"].state == '3') TxtQuestion = TxtQuestion + " uit bij voorwaarden] ?"
          if (states["counter.tuin_hoeklamp_do"].state == '4') TxtQuestion = TxtQuestion + "aan bij stop voorwaarden] ?"
          if (states["counter.tuin_hoeklamp_do"].state == '5') TxtQuestion = TxtQuestion + "uit bij stop voorwaarden] ?"
          return TxtQuestion
      ]]]
  action: call-service
  service: pyscript.scenedefinition_insert
  service_data:
    DefName: status_tuin_hoeklamp
    A_or_B: A
    ActionNr: |
      [[[
        return (states["counter.tuin_hoeklamp_action"].state)
      ]]]
    WhenNr: |
      [[[
        return (states["counter.tuin_hoeklamp_when"].state)
      ]]]
    DoNr: |
      [[[
        return (states["counter.tuin_hoeklamp_do"].state)
      ]]]
    RGB: |
      [[[ 
        if (states["light.tuin_hoeklamp"].attributes.color_mode == 'hs') return states["light.tuin_hoeklamp"].attributes.rgb_color
        if (states["light.tuin_hoeklamp"].attributes.color_mode != 'hs') return '[ 0, 0, 0]'
      ]]]
    Brightness: |
      [[[ 
        return states["light.tuin_hoeklamp"].attributes.brightness 
      ]]]
    Color_Temp: |
      [[[ 
        if (states["light.tuin_hoeklamp"].attributes.color_mode == 'color_temp') return states["light.tuin_hoeklamp"].attributes.color_temp_kelvin
        if (states["light.tuin_hoeklamp"].attributes.color_mode != 'color_temp') return '0'
      ]]]
tap_action:
  action: call-service
  service: counter.increment
  service_data:
    entity_id: counter.tuin_hoeklamp_action
theme: kibibit-dark-cards
state:
  - value: 1
    icon: mdi:hand-front-left
    styles:
      icon:
        - color: rgb(128,128,128,100%)
  - value: 2
    icon: mdi:cog
    styles:
      icon:
        - animation: rotating 2s linear infinite
        - color: rgb(128,128,255,100%)
  - value: 3
    icon: mdi:bed
    styles:
      icon:
        - color: rgb(95,255,96,100%)
  - value: 4
    icon: mdi:motion-sensor
    styles:
      icon:
        - color: rgb(255,64,64,100%)
  - value: 5
    icon: mdi:account-circle
    styles:
      icon:
        - color: rgb(255,255,96,100%)
  - value: 6
    icon: mdi:weather-sunset
    styles:
      icon:
        - animation: blink 1.5s linear infinite
        - color: rgb(255,128,64,100%)
  - value: 7
    icon: mdi:shield-check
    styles:
      icon:
        - color: rgb(255,0,0,100%)
styles:
  card:
    - background-color: rgb(0,0,0,25%)
    - border-radius: 10px
  icon:
    - height: 54px
card_mod:
  style: |
    ha-card { 
      margin-top: -8px;
    }


Button 2 – When:

show_name: false
show_icon: true
type: custom:button-card
entity: counter.tuin_hoeklamp_when
double_tap_action:
  action: call-service
  service: input_boolean.toggle
  service_data:
    entity_id: input_boolean.tuin_hoeklamp_dir
hold_action:
  confirmation:
    text: |
      [[[
          var TxtQuestion = "Instellingen gebruiken voor status ["
          if (states["counter.tuin_hoeklamp_action"].state == '1') TxtQuestion = TxtQuestion + "manueel/"
          if (states["counter.tuin_hoeklamp_action"].state == '2') TxtQuestion = TxtQuestion + "automatisch/"
          if (states["counter.tuin_hoeklamp_action"].state == '3') TxtQuestion = TxtQuestion + "iedereen in bed/"
          if (states["counter.tuin_hoeklamp_action"].state == '4') TxtQuestion = TxtQuestion + "iedereen uit bed/"
          if (states["counter.tuin_hoeklamp_action"].state == '5') TxtQuestion = TxtQuestion + "sommigen in- & uit bed/"
          if (states["counter.tuin_hoeklamp_action"].state == '6') TxtQuestion = TxtQuestion + "zon op-onder/"
          if (states["counter.tuin_hoeklamp_action"].state == '7') TxtQuestion = TxtQuestion + "alarm/"
          if (states["counter.tuin_hoeklamp_when"].state == '1') TxtQuestion = TxtQuestion + "altijd/"
          if (states["counter.tuin_hoeklamp_when"].state == '2') TxtQuestion = TxtQuestion + "dag - vrij helder/"
          if (states["counter.tuin_hoeklamp_when"].state == '3') TxtQuestion = TxtQuestion + "dag - normaal/"
          if (states["counter.tuin_hoeklamp_when"].state == '4') TxtQuestion = TxtQuestion + "dag - overtrokken/"
          if (states["counter.tuin_hoeklamp_when"].state == '5') TxtQuestion = TxtQuestion + "donker - vrij helder/"
          if (states["counter.tuin_hoeklamp_when"].state == '6') TxtQuestion = TxtQuestion + "donker - normaal/"
          if (states["counter.tuin_hoeklamp_when"].state == '7') TxtQuestion = TxtQuestion + "donker - meest donker/"
          if (states["counter.tuin_hoeklamp_do"].state == '1') TxtQuestion = TxtQuestion + "alle bewegingen] ?"
          if (states["counter.tuin_hoeklamp_do"].state == '2') TxtQuestion = TxtQuestion + " aan bij voorwaarden] ?"
          if (states["counter.tuin_hoeklamp_do"].state == '3') TxtQuestion = TxtQuestion + " uit bij voorwaarden] ?"
          if (states["counter.tuin_hoeklamp_do"].state == '4') TxtQuestion = TxtQuestion + "aan bij stop voorwaarden] ?"
          if (states["counter.tuin_hoeklamp_do"].state == '5') TxtQuestion = TxtQuestion + "uit bij stop voorwaarden] ?"
          return TxtQuestion
      ]]]
  action: call-service
  service: pyscript.scenedefinition_insert
  service_data:
    DefName: status_tuin_hoeklamp
    A_or_B: A
    ActionNr: |
      [[[
        return (states["counter.tuin_hoeklamp_action"].state)
      ]]]
    WhenNr: |
      [[[
        return (states["counter.tuin_hoeklamp_when"].state)
      ]]]
    DoNr: |
      [[[
        return (states["counter.tuin_hoeklamp_do"].state)
      ]]]
    RGB: |
      [[[ 
        if (states["light.tuin_hoeklamp"].attributes.color_mode == 'hs') return states["light.tuin_hoeklamp"].attributes.rgb_color
        if (states["light.tuin_hoeklamp"].attributes.color_mode != 'hs') return '[ 0, 0, 0]'
      ]]]
    Brightness: |
      [[[ 
        return states["light.tuin_hoeklamp"].attributes.brightness 
      ]]]
    Color_Temp: |
      [[[ 
        if (states["light.tuin_hoeklamp"].attributes.color_mode == 'color_temp') return states["light.tuin_hoeklamp"].attributes.color_temp_kelvin
        if (states["light.tuin_hoeklamp"].attributes.color_mode != 'color_temp') return '0'
      ]]]
tap_action:
  action: call-service
  service: counter.increment
  service_data:
    entity_id: counter.tuin_hoeklamp_when
theme: kibibit-dark-cards
state:
  - value: 1
    icon: mdi:theme-light-dark
    styles:
      icon:
        - color: rgb(255,128,0,100%)
        - animation: draai 3s linear infinite
  - value: 2
    icon: mdi:weather-sunny
    styles:
      icon:
        - color: rgb(255,255,255,100%)
  - value: 3
    icon: mdi:weather-sunny
    styles:
      icon:
        - color: rgb(255,255,0,100%)
  - value: 4
    icon: mdi:weather-sunny
    styles:
      icon:
        - color: rgb(255,128,0,100%)
  - value: 5
    icon: mdi:weather-night
    styles:
      icon:
        - color: rgb(255,128,0,100%)
  - value: 6
    icon: mdi:weather-night
    styles:
      icon:
        - color: rgb(255,0,255,100%)
  - value: 7
    icon: mdi:weather-night
    styles:
      icon:
        - color: rgb(64,96,255,100%)
styles:
  card:
    - background-color: rgb(0,0,0,25%)
    - border-radius: 10px
  icon:
    - color: rgb(255,128,0,100%)
    - height: 54px
extra_styles: |
  @keyframes draai {  
    from {transform: rotateY(0deg);}  
    to {transform: rotateY(360deg);}
  }
card_mod:
  style: |
    ha-card { 
      margin-top: -8px;
    }


Button 3 – Performance:

show_name: false
show_icon: true
type: custom:button-card
entity: counter.tuin_hoeklamp_do
double_tap_action:
  action: call-service
  service: input_text.set_value
  service_data:
    value: status_tuin_hoeklamp
    entity_id: input_text.helper_scenedefinitions
hold_action:
  confirmation:
    text: |
      [[[
        var TxtQuestion = "Instellingen gebruiken voor status ["
        if (states["counter.tuin_hoeklamp_action"].state == '1') TxtQuestion = TxtQuestion + "manueel/"
        if (states["counter.tuin_hoeklamp_action"].state == '2') TxtQuestion = TxtQuestion + "automatisch/"
        if (states["counter.tuin_hoeklamp_action"].state == '3') TxtQuestion = TxtQuestion + "iedereen in bed/"
        if (states["counter.tuin_hoeklamp_action"].state == '4') TxtQuestion = TxtQuestion + "iedereen uit bed/"
        if (states["counter.tuin_hoeklamp_action"].state == '5') TxtQuestion = TxtQuestion + "sommigen in- & uit bed/"
        if (states["counter.tuin_hoeklamp_action"].state == '6') TxtQuestion = TxtQuestion + "zon op-onder/"
        if (states["counter.tuin_hoeklamp_action"].state == '7') TxtQuestion = TxtQuestion + "alarm/"
          if (states["counter.tuin_hoeklamp_when"].state == '1') TxtQuestion = TxtQuestion + "altijd/"
          if (states["counter.tuin_hoeklamp_when"].state == '2') TxtQuestion = TxtQuestion + "dag - vrij helder/"
          if (states["counter.tuin_hoeklamp_when"].state == '3') TxtQuestion = TxtQuestion + "dag - normaal/"
          if (states["counter.tuin_hoeklamp_when"].state == '4') TxtQuestion = TxtQuestion + "dag - overtrokken/"
          if (states["counter.tuin_hoeklamp_when"].state == '5') TxtQuestion = TxtQuestion + "donker - vrij helder/"
          if (states["counter.tuin_hoeklamp_when"].state == '6') TxtQuestion = TxtQuestion + "donker - normaal/"
          if (states["counter.tuin_hoeklamp_when"].state == '7') TxtQuestion = TxtQuestion + "donker - meest donker/"
        if (states["counter.tuin_hoeklamp_do"].state == '1') TxtQuestion = TxtQuestion + "alle bewegingen] ?"
        if (states["counter.tuin_hoeklamp_do"].state == '2') TxtQuestion = TxtQuestion + " aan bij voorwaarden] ?"
        if (states["counter.tuin_hoeklamp_do"].state == '3') TxtQuestion = TxtQuestion + " uit bij voorwaarden] ?"
        if (states["counter.tuin_hoeklamp_do"].state == '4') TxtQuestion = TxtQuestion + "aan bij stop voorwaarden] ?"
        if (states["counter.tuin_hoeklamp_do"].state == '5') TxtQuestion = TxtQuestion + "uit bij stop voorwaarden] ?"
        return TxtQuestion
      ]]]
    action: call-service
  action: call-service
  service: pyscript.scenedefinition_insert
  service_data:
    DefName: status_tuin_hoeklamp
    A_or_B: A
    ActionNr: |
      [[[
        return (states["counter.tuin_hoeklamp_action"].state)
      ]]]
    WhenNr: |
      [[[
        return (states["counter.tuin_hoeklamp_when"].state)
      ]]]
    DoNr: |
      [[[
        return (states["counter.tuin_hoeklamp_do"].state)
      ]]]
    RGB: |
      [[[ 
        if (states["light.tuin_hoeklamp"].attributes.color_mode == 'hs') return states["light.tuin_hoeklamp"].attributes.rgb_color
        if (states["light.tuin_hoeklamp"].attributes.color_mode != 'hs') return '[ 0, 0, 0]'
      ]]]
    Brightness: |
      [[[ 
        return states["light.tuin_hoeklamp"].attributes.brightness 
      ]]]
    Color_Temp: |
      [[[ 
        if (states["light.tuin_hoeklamp"].attributes.color_mode == 'color_temp') return states["light.tuin_hoeklamp"].attributes.color_temp_kelvin
        if (states["light.tuin_hoeklamp"].attributes.color_mode != 'color_temp') return '0'
      ]]]
tap_action:
  action: call-service
  service: counter.increment
  service_data:
    entity_id: counter.tuin_hoeklamp_do
theme: kibibit-dark-cards
state:
  - value: 1
    icon: mdi:sync
  - value: 2
    icon: mdi:arrow-right-bold
  - value: 3
    icon: mdi:arrow-right-bold-outline
  - value: 4
    icon: mdi:square
  - value: 5
    icon: mdi:square-outline
styles:
  card:
    - background-color: rgb(0,0,0,25%)
    - border-radius: 10px
  icon:
    - height: 54px
    - color: rgb(160,160,160,100%)
card_mod:
  style: |
    ha-card { 
      margin-top: -8px;
    }

9. How do this actually work ? – workflow description:
1. Every click of a button on this “package” of device-controls, is re-directed to a pyscript. It is therefore that no device will respond, if no definition is made for a certain action.
2. I had to find some work-arounds, for easyness of HTML showing of data. After all, there is no direct (file)link between the docker container running the MySQL instance and HA itself.

– Whenever an action is triggered (automated or manual), scenedefinition_automation.py is triggered.
Depending on the actual action, the corresponding record is read from the MySQL database, and the device-properties are set accordingly. (RGB value / brightness / color temperature)
When the device needs to be turned off, the brightness level is first brought back to a minimum level, and after this, the device is turned off.
This is to start the device NOT with full brightness, when turned on again. (We use this automations to trigger lights at night, at a much lower brightness)
When no definition is found for the current action, nothing is triggered of course.

– Whenever an “info request” is triggered, scenedefinition_info.py is run: first the CSV-file with previous results is deleted from the MySQL docker container.
Next, a query is run to retrieve all definitions for the queried device. This result is put into a CSV file (this is standard functionality in MySQL)
Next, this CSV file is copied from the docker container, onto the WWW location of the HA instance.
Finally, an HTML file is opened, with a javascript function in it, that parses the CSV data into the HTML, within an HTML table.
This HTML file is shown on every screen with the HA interface on it. This used the browser_mod HACS add-on.
You can extend the functionality of this PyScript, to only show this info onto specific target screens. Check out the Browser Mod documentation for more info on this …

– Whenever you trigger the deletion of all scene definitions for a certain device, the scenedefinition_delete_bulk.py script gets launched.
This simply deletes all definitions for the current device, from the MySQL database.

– And finally, whenever you created a new scene-definition (RGB / Brightness/ Color Temperature / Action/When/Peformance) and request to save it, scenedefinition_insert.py gets called.
Make sure that your device is turned on, in the state that you want it to be activated. The script will insert all parameters into the MySQL database.

10. Some after-toughts ….
This was one of my first home automations I wrote, just having HA running for 2 weeks or so.
I’m sure this system has some shortcomings and that some things can be done better / shorter / less complicated !
But I hope you get an idea of what is possible with some combinations of default and HACS-related add-ons.
If you have some ideas or toughts that you would like to share with me, please feel free to contact me 🙂
(You do find me e-mail address on the bottom of each page)

This entry was posted in Blog, Home Assistant, Tutorials. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *