Migrating from Hubitat Eleveation is a huge step to do

Hello to all,

I’m a newbie to Home Assistant and I have to admit that I struggle with my first steps to test Home Assistant.

I created my first challenging automation by moving over an automation from HE. I did build this automation completely with the HE graphical interface, including variables.

This rule steers two Philips Play RGBW Lights and changes all 5 seconds the colour transition of every light as an opposite colour (HS 360 & HS 180, then HS 0 & HS 190, then HS 200 & HS 10 …)

I created this YAML code mainly with the graphical interface of Home Assistant but struggled at some points where I had to switch to YAML and it took 5 hours of search and trial to get it work.

alias: office_play_colorrotation
description: Rotate colors of the two hue play lights
trigger:
  - platform: state
    entity_id:
      - input_boolean.office_switch_playcolorrotation_virtual
    to: "on"
condition: []
action:
  - service: input_number.set_value
    data:
      value: 0
    target:
      entity_id:
        - input_number.var_office_play01_color
  - service: input_number.set_value
    data:
      value: 180
    target:
      entity_id: input_number.var_office_play02_color
  - repeat:
      sequence:
        - if:
            - condition: numeric_state
              entity_id: input_number.var_office_play01_color
              above: 359
          then:
            - service: input_number.set_value
              data:
                value: 0
              target:
                entity_id: input_number.var_office_play01_color
          else:
            - service: input_number.increment
              data: {}
              target:
                entity_id: input_number.var_office_play01_color
        - if:
            - condition: numeric_state
              entity_id: input_number.var_office_play02_color
              above: 359
          then:
            - service: input_number.set_value
              data:
                value: 0
              target:
                entity_id: input_number.var_office_play02_color
          else:
            - service: input_number.increment
              data: {}
              target:
                entity_id: input_number.var_office_play02_color
        - delay:
            hours: 0
            minutes: 0
            seconds: "{{ states(\"input_number.var_office_Play_SwitchTime\") | int }}"
            milliseconds: 0
        - service: light.turn_on
          data:
            hs_color: "{{ states(\"input_number.var_office_play01_color\") | int }},100"
          target:
            entity_id: light.office_light_hueplay01
        - service: light.turn_on
          data:
            hs_color: "{{ states(\"input_number.var_office_play02_color\") | int }},100"
          target:
            entity_id:
              - light.office_light_hueplay02
      while:
        - condition: state
          entity_id: input_boolean.office_switch_playcolorrotation_virtual
          state: "on"
  - service: light.turn_off
    data: {}
    target:
      entity_id:
        - light.office_light_hueplay01
        - light.office_light_hueplay02
mode: single

The main challenge were variables, and I did not find a way to use variables. I created 3 numeric helpers (not the coolest way) but how can a newbie find things like that :

"{{ states(\"input_number.var_office_play02_color\") | int }},100"

simply to get a value from a helper. Lost much google time on that and don’t still understand all. I’m a programmer, but this syntax is weird.

Here is an example of my test dashboard :wink:

Maybe there are better ways to do that. Any help or advice is appreciated. Thanks.

Variables

The scope of Home Assistant’s script variables is limited to the script or automation where they are defined (i.e. they’re not global variables). If you need to define a value that must be referenced by multiple scripts/automations, the standard practice is to use an Input helper (Input Number, Input Boolean, Input Datetime, etc) because it can also be set via the UI.

For your application, you’re better off using an Input Number because you’re incrementing its value within the repeat. A script variable doesn’t allow for that.

You can also create a Trigger-based Template Sensor that behaves like a global variable.
There’s also a third-party custom integration that provides a “global variables” feature.

I recommend you review the following documentation:

Script integration
Script Syntax
Templating


Regarding this:

"{{ states(\"input_number.var_office_play02_color\") | int }},100"

Inner and outer quotes cannot use the same quotation character. Therefore the inner double-quotes were “escaped” by prepending them with a backslash.

Here are examples of acceptable quotation:

"{{ states('input_number.var_office_play02_color') | int }},100"
'{{ states("input_number.var_office_play02_color") | int }},100'
'{{ states(''input_number.var_office_play02_color'') | int }},100'

Thank you for your reply.

The two variables for colour could be local, as I set their starting values to zero and 180 before entering the loop. I inserted a third variable for the wait time, but this is mainly for debugging (1s) and to be able to use the automation as a template for another one if I want to create another animation. Finally, all three could be local. I did find no way to declare them and to use them. There is a field inside the graphical interface, but it’s a text input ???

Yes, and that is weird. I am sure I did not enter twice the same quotes, and after changing them to ’ …" … " …’ they switched back to the same " quotes after saving with the escape characters. The automation still worked correctly. So I tried " … ’ … ’ … " and this time they seem to stay as that in the editor. It’s strange that regardless of this same quotes after saving, the automation worked every time.

The Automation Editor doesn’t expose all of Home Assistant’s scripting features. The reason for this is a blend of “insulate novice user from complexity” and “no nobody has volunteered to implement it”.

Upon clicking “Save” in the Automation Editor, it will format the YAML according to its formatting rules. These rules can sometimes alter, even break, your code. For example, if you configure a variable to contain a boolean value like true, it’ll be converted to a string value "true". If you add a YAML comment, it will be eliminated.

To gain complete control over YAML formatting and features, you have to avoid using the Automation Editor and compose automations/scripts using a text editor (obviously this is only feasible after you become more familiar with YAML and how its used to create an automation or script). The usual suggestion is to use Visual Studio Code along with Config Helper for Home Assistant (both are available as a single add-on). However, you should then instruct Home Assistant to keep your “manually created” automations separate from the ones created via the Automation Editor (which, by default, stores them in the automation.yaml file). Instructions for doing that are found at the very bottom of Splitting your configuration.

OK, that makes HA not half as interesting as I thought. Someone told me that HA is not the same I tried some years ago and has now a graphical interface for nearly everything. If this interface hides some features, like simple variables, that’s not nice, but if it is able to break code than it is not worth much.

Don’t misunderstand me, and maybe I will continue to play with it. As a developer, I’m used to learning code (unfortunately until now not Python and YAML—More XML and JSON :wink: ) but I looked at a pleasant system to make a tutorial for lambda users from beginning to expert. Hubitat seems the better alternative, regardless of some flaws. But who knows, maybe I will change my mind with the help of the community :wink: Your support is greatly appreciated.

If it lets you achieve your home automation goals, you should continue using it.