Help with Templating (input_number to set brightness when running script)

I made the move from a homebridge setup to home assistant running on a raspberry pi last week. There’s been a small learning curve but, on the whole, it’s an easy platform to use—and gives way more control over automations (but you already know that).

Anyway, due to the minor differences on how Hass works, I’ve set up my scenes as scripts but I didn’t like the way they were displayed. I played around with input_booleans but, in the end, settled on input_selects (to save real estate). I have multiple input_selects (for different rooms) and an automation for each scene which is triggered if any of the input_selects chooses the scene. Maybe someone has a better approach?

I’ve also set a ‘master brightness’ input_number with the aim of adjusting the brightness for the entire house (which can be automated depending on light level, time of day, etc). I’ve created a scene/script which sets the hue & saturation and checks the value of the master brightness to set the scene brightness. When I use templating to look up the value, nothing happens. When I explicitly state the value, it works fine.

I’ve checked the templating format using the web interface and everything appears to work fine. I also have another script that uses the exact same method to set a timer and that works fine so I’m not sure where I’ve gone wrong with this one. I need a fresh pair of eyes.

Here’s the script that runs the scene:

    peaches_and_cream_script:
      alias: Peaches & Cream (script)
      sequence:
      - data:
          brightness_pct: '{{ states.input_number.slider2.state | int }}'
          entity_id: light.stuarts_desk_lamp
          hs_color:
          - 31
          - 42
        service: light.turn_on
      - data:
          brightness_pct: '90'
          entity_id: light.right_uplighter
          hs_color:
          - 35
          - 59
        service: light.turn_on
      - data:
          brightness_pct: '90'
          entity_id: light.left_uplighter
          hs_color:
          - 33
          - 67
        service: light.turn_on
      - data:
          brightness_pct: '90'
          entity_id: light.fionas_desk_lamp
          hs_color:
          - 37
          - 44
        service: light.turn_on
      - data:
          entity_id: switch.fairy_lights
        service: switch.turn_on

Here’s the automation that triggers when any of my input_selects are used to choose the Peaches & Cream scene:

    - id: '1526473407210'
      alias: Peaches & Cream Input Select
      trigger:
      - entity_id: input_select.fav_scenes
        platform: state
        to: Peaches & Cream
      - entity_id: input_select.lr_scenes
        platform: state
        to: Peaches & Cream
      condition: []
      action:
      - service: script.peaches_and_cream_script
        service: script.1526475620973

All the second script does is set the input select back to the default option.

Here’s an example of one of my input_selects:

     fav_scenes:
      name: Favourite Scenes
      options:
       - ' '
       - Peaches & Cream
       - Kitchen Colours
       - Relaxing
       - Reading in Bed
       - Footpath Chalk
       - Tropical
       - Night Night
      icon: mdi:floor-plan
      initial: ' '

And finally, the code for the input_number:

    slider2:
     name: "Master Brightness"
     initial: 50
     min: 0
     max: 100
     step: 1

You need to use a data_template instead of a data in your script.

      sequence:
      - data_template:
          brightness_pct: '{{ states.input_number.slider2.state | int }}'
          entity_id: light.stuarts_desk_lamp
          hs_color:
          - 31
          - 42
        service: light.turn_on

If you have 2 services you are running, both need a -

      action:
      - service: script.peaches_and_cream_script
      - service: script.1526475620973
2 Likes

Awesome. I knew it would be down to a stupid oversight, or something. Thanks! The second part was just a typo.

If you are really feeling lucky, you can make one automation to execute them all!

    - alias: Input Select Scene Executer
      trigger:
      - entity_id: input_select.fav_scenes
        platform: state
      - entity_id: input_select.lr_scenes
        platform: state
      condition:
        contidion: template
        value_template: {{ trigger.to_state.state != ' ' }}
      action:
      - service_template: >
          {% set my_map = {'Peaches & Cream':'script.peaches_and_cream_script',
                           'Kitchen Colours':'script.kitchen_colours_script',
                           'Relaxing':'script.relaxing_script',
                           'Reading in Bed':'script.reading_in_bed_script',
                           'Footpath Chalk':'script.footpath_chalk_script',
                           'Tropical':'script.tropical_script',
                           'Night Night':'script.night_night_script'}
         {% if trigger.to_state.state in my_map %}
           {{ my_map[trigger.to_state.state] }}
         {% else %}
            script.dummy
         {% endif %}
      - service: script.1526475620973

You’ll need to create a dummy script with that does something, anything really. Doesn’t matter what you do as long as the script contains a service you should be good. Example, you could have it turn on an input boolean, never display the boolean though.

The dummy script is needed so you don’t get an error when home assistant is starting or the input select option screws up.

1 Like

You’ve literally just gifted me a Saturday—this was going to be my next line of investigation. I’ll try to implement it tomorrow—you can probably expect my call!

HI @petro,

you got me triggered here…

please see this input_select calling scripts i have, would that need the above improvements also? Thinking of the dummy script, and the set my_map…

input_select:
  activity:
    name: Activity selection
    icon: mdi:home
    options:
    - Opstart
    - Opstaan
    - Aan de slag
    - Uit huis
    - Gym
    - Gaming
    - Selamat makan
    - Home theater
    - Naar bed
    - Arrive home
#    initial: Opstart 

and

automation:

  - alias: Activity selection
    id: 'Activity selection'
    hide_entity: True
    initial_state: 'on'
    trigger:
      platform: state
      entity_id: input_select.activity
    action:
      - service_template: >
          {% if is_state ('input_select.activity','Opstart') %} script.opstart
          {% elif is_state ('input_select.activity', 'Opstaan') %} script.opstaan
          {% elif is_state ('input_select.activity','Aan de slag') %} script.aan_de_slag
          {% elif is_state ('input_select.activity', 'Home theater') %} script.home_theater
          {% elif is_state ('input_select.activity','Gym') %} script.gym
          {% elif is_state ('input_select.activity','Gaming') %} script.gaming
          {% elif is_state ('input_select.activity', 'Selamat makan') %} script.selamat_makan
          {% elif is_state ('input_select.activity', 'Uit huis') %} script.uit_huis
          {% elif is_state ('input_select.activity', 'Naar bed') %} script.naar_bed
          {% elif is_state ('input_select.activity', 'Arrive home') %} script.arrive_home
          {% endif %}
      - delay: 00:00:02
      - service: python_script.summary
#        data_template:
#          event: "{{ trigger.event }}"

im having issues at startup, where the scripts are triggered but not really do their job completely.

especially the accompanying input_select.mode:

automation:
  - alias: 'Mode selection'
    id: 'Mode selection'
    hide_entity: True
    initial_state: 'on'
    trigger:
      platform: state
      entity_id: input_select.mode
    action:
      - service: homeassistant.turn_on
        data_template:
          entity_id: >
            {% if is_state ('input_select.mode','Normal') %} script.mode_normal
            {% elif is_state ('input_select.mode','Full house') %} script.mode_full_house
            {% elif is_state ('input_select.mode','Kiosk') %} script.mode_kiosk
            {% elif is_state ('input_select.mode','Developer') %} script.mode_developer
            {% elif is_state ('input_select.mode','Vacation') %} script.mode_vacation
            {% elif is_state ('input_select.mode','Locked') %} script.mode_locked
            {% endif %}
      - delay: 00:00:02
      - service: python_script.summary
#        data_template:
#          event: "{{ trigger.event }}"

thanks for having a look!
Marius

Sorry, I just remember this post.

You may be able to avoid a ‘dummy script’ if you add a condition prior to executing the map. Also, you are pretty organized with your naming conventions, you could forgo the map and use some string editing.

automation:
  - alias: 'Mode selection'
    id: 'Mode selection'
    hide_entity: True
    initial_state: 'on'
    trigger:
      platform: state
      entity_id: input_select.mode
    condition:
      condition: template
      value_template: >
        {{ states('input_select.mode') in ['Normal','Full house','Kiosk','Developer','Vacation','Locked'] }}
    action:
      - service: homeassistant.turn_on
        data_template:
          entity_id: "{{ 'script.mode_' + states('input_select.mode') | lower | replace(' ','_') }}"
      - delay: 00:00:02
      - service: python_script.summary
#        data_template:
#          event: "{{ trigger.event }}"

EDIT: simplified it some more.

1 Like

thanks!
this is my next step in editing Yaml. Using these lists is still a bit new for me, after having been so meticulously meticulous with building yaml listing the separate entities.

Will give this a new impuls!

btw, I have found out that the fact this specific automation didnt kick in every on startup was also a timing issue. It calls a script calling a script, also notifying feedback.
On @balloob’s advice Ive revisited all of these scripts, and now trust restore_state to take care of the 2 automations I have at startup, mode selection and activity selection.

The specific sub script that was called here when selecting developer mode (showing several dedicated groups, and hiding them when the other options were called) is now being called directly as follows:

- alias: 'No dev tools at startup HA'
  id: 'No dev tools at startup HA'
  initial_state: 'on'
  trigger:
    platform: homeassistant
    event: start
  condition:
  action:
    - delay: '00:00:15'
    - condition: state
      entity_id: binary_sensor.mode_developer
      state: 'off'
    - service: group.set_visibility
      entity_id: group.developer
      data:
        visible: False
    - service: group.set_visibility
      entity_id: group.developer_links
      data:
        visible: False
#    - delay: '00:00:05'
#    - service: script.mode_normal_direct
#    - delay: '00:00:03'
#    - service: script.opstart_direct
    - condition: state
      entity_id: input_boolean.notify_developing
      state: 'on'
    - service: notify.notify
      data:
        message: >
          Dev mode - Dev mode visibility set to false at setup

Made an extra notify.Dev_mode so I dont hit the max notifications each day :wink:

exactly why do i need the condition here? the action template selects only valid states doesnt it? So no exception catching, or ‘else’ is necessary?

Because you will get an error if your data_template has an empty or invalid entity_id. Templates do not only select valid states, it will execute whatever code you supply it.

that makes sense, (from a computers point of view…) :wink:
btw changed the above automation into this, practically obliterating the need for the other input_select, which was till now mainly used, for selecting the dev mode tools or not…

have to develop the Modes now priority 1! (or cut code heavily of course)

  - alias: 'Dev Mode selection'
    id: 'Dev Mode selection'
    initial_state: 'on'
    trigger:
      platform: state
      entity_id: input_select.mode
    action:
      - service: group.set_visibility
        entity_id: 
          - group.developer
          - group.developer_links
        data_template:
          visible: "{{ is_state('input_select.mode', 'Developer') }}"

might as well combine the 2 and add the homeassistant: start trigger here, and be done with it.

1 Like

Nice, looks good.

1 Like

yep, and works beautifully and swift, better than before …
next up, trying the View: true bit of a group to change dynamically:

service: group.set
 data_template:
    object_id: Developer
    view: "{{ is_state('input_select.mode', 'Developer') }}"
    visible: "{{ is_state('input_select.mode', 'Developer') }}"

Looking at the source code, i would think it would work. It’s at the same level as visible on a group object. Give it a whirl. I’m not sure how it would behave in your browser because browsers can be dumb.

they’re dumb…
no luck just yet. No errors either, and even Hassio is connecting, so that’s rather noteworthy. Before with even the slightest thing Hassio would fail, and now, with a more substantial issue, is doesnt. All the better.

Ive tried the in the regular group definition, but that errored out.

Having it in the automation:

  - alias: 'Dev Mode selection'
    id: 'Dev Mode selection'
    initial_state: 'on'
    trigger:
      platform: state
      entity_id: input_select.mode
    action:
      - service: group.set_visibility
        entity_id:
          - group.developer_links
        data_template:
          visible: "{{ is_state('input_select.mode', 'Developer') }}"
      - service: group.set
        data_template:
          object_id: Developer
          view: "{{ is_state('input_select.mode', 'Developer') }}"
          visible: "{{ is_state('input_select.mode', 'Developer') }}"

passes all checks, but has no results, at least the second service, which is whats all about of course.
Might have to try something else.
Cheers,
Marius

you clear cache and refresh page (ctrl+f5)?

yes, sure, even loaded Chrome, and in incognito mode. Nothing happening …

I wonder if they have methods that handle the visibilty for the visible property.

i can confirm this works fine:

we must be able to do that from an automation or script…

have you tried it in an automation but hardcoded? So have it off, fire the auotmation and have it turn on. Just to verify that it could work? It could just not allow templating.