Light Control Problem - Poss Syntax Issue?

I’m hoping that someone can look over my code and see what I’m doing wrong.
Some background I started with HA Nov 4th 2017 (so 5 weeks ago) and this is where I have gotten.
I am just testing stuff out and building my UI, I only have a Z-wave light dimmer at the moment but if I can get that exactly as I want the rest should be a doddle ; - ) I’ve sorted Weather, Tabs, Cards, Default View, Zones and Customisation. So I’m progressing a pace thanks mainly to reading other peoples issues and solutions.
But … my biggest bugbear was people leaving the cloakroom light on.
I used the dimmer in a test rig and was very impressed with out of box functionality, low voltage switch loop for light switch toggling on change of state (good for 2 or even 3 or 4 way switches), paired with Z-stick, set notification to basic and can both act on switch and see result, from HA.
So created an automation to turn light off after 10 mins :-

In automations.yaml

- alias: Light_F0_CR_Tmr_OFF # when light comes on timer starts to turn it off again
  trigger:
    platform: state
    entity_id: light.aeotec_dsc19103_micro_smart_dimmer_2nd_edition_level
    to: 'on'
    for:
      #minutes: 10
      seconds: 600
  action:
    service: light.turn_off
    entity_id: light.aeotec_dsc19103_micro_smart_dimmer_2nd_edition_level

Works great ; - )
Now I wanted to be able to change the light level, okay use a slider and have the change of slider control the brightness.
So slider is 0 to 255 and I set initial value to 152 (60%), again, works great !

In input_number.yaml

light_f0_cr_ctrl:
  name: CRL Brightness
  initial: 152
  min: 0
  max: 255
  step: 1

And in automations.yaml I have another automation, so that it comes on at the right level-

- alias: Light_F0_CR_To_On # light comes on at set brightness
  trigger:
    platform: state
    entity_id: light.aeotec_dsc19103_micro_smart_dimmer_2nd_edition_level
    to: 'on'
  action:
    service: light.turn_on
    entity_id: light.aeotec_dsc19103_micro_smart_dimmer_2nd_edition_level
    data_template:
      brightness: '{{ input_number.light_f0_cr_ctrl.state | int }}'

Okay so my next issue is bathroom light at night. I have to stumble in the dark as it will blind me and wake the wife.
So how about at midnight, I set the brightness slider to a lower level, then back up at dawn ? So extra automations for my levels (remember it’s a test rig)

- alias: Light_F0_CR_ToD_LvlAdj1 # adjust light level
  trigger:
    platform: time
    at: '00:30:00'
  action:
    service: input_number.light_f0_cr_ctrl
    data:
      value: 64
- alias: Light_F0_CR_ToD_LvlAdj2 # adjust light level
  trigger:
    platform: sun
    event: sunrise
    offset: '00:30:00'
  action:
    service: input_number.light_f0_cr_ctrl
    data:
      value: 192
- alias: Light_F0_CR_ToD_LvlAdj3 # adjust light level
  trigger:
    platform: sun
    event: sunset
    offset: '00:30:00'
  action:
    service: input_number.light_f0_cr_ctrl
    data:
      value: 128

Now, what I think I need is an automation where it changes the level (slider) value, and if the light is on, it writes that to the light

- alias: Light F0 Cloak Room Brightness # if level changed light comes on to that level, do I need this ?
  trigger:
    platform: state
    entity_id: input_number.light_f0_cr_ctrl
  condition:
    - condition: state
      entity_id: light.aeotec_dsc19103_micro_smart_dimmer_2nd_edition_level
      state: 'on'
  action:
    service: light.turn_on
    **# Note the use of 'data_template:' below for an evaluated variable, rather than the normal 'data:' when using a given value**
    entity_id: light.aeotec_dsc19103_micro_smart_dimmer_2nd_edition_level
    data_template:
      brightness: '{{ trigger.to_state.state | int }}'

This is where it all went wrong and the light does not come on at the slider value, just the initial value, and the slider does not change at all.
And though the ‘{{ input_number.light_f0_cr_ctrl.state | int }}’ worked in the code to spot changes in the slider and update lamp output it does not work in the template checker, so is that a limited sandbox ??? why ?
I hope this is just a simple mistake, please yell if you spot it
Cheers - and thanks just for reading !
Mutt

I think the only times that will work is 00:30 and sunset and sunrise +30 because you are triggering on the input_number state change with the condition that the light is already on. So if the light is off (which at all of those time it probably will be) nothing will happen. I think you need to do it the other way round or turn it on and set the brightness when you change the input number then turn it off so it’s in the default state you want.

Keith,
Sorry, I must not have made myself clear.
The very last bit of code seems to work (pretty sure), it’s the block of automation before that, they do not update the value of the slider. Though they pass the syntax checker, that does not mean it’s workable code : - ((((
That’s why I was complaining about the template checker (it doesn’t recognise ‘input_number.’ as something it can evaluate.
I’m sure I’ve read that people use sliders to store ‘numbers’ that can change.
I’m happy with that (even on power failure and reboot) as the initial value should not be far off the mark and the worst case is it will only be wrong to the next event level change. (unless the hub crashes, then I’ll just have to live with whatever the dimmer has stored at that time).
Mutt

OK, sorry I kind of ignored that as I thought that worked :stuck_out_tongue:

 - service: input_number.set_value
   data:
       entity_id: input_number.bed_max_temp
       value: 25

The above should work, basically you need to call set_value to set the input_number

1 Like

Keith,
That fixed it, fantastic !
The modified code is now (again living in automations) :-

- alias: Light_F0_CR_ToD_LvlAdj1 # adjust light level
  trigger:
    platform: time
    at: '00:30:00'
  action:
    - service: input_number.set_value
      data:
        entity_id: input_number.light_f0_cr_ctrl
        value: 64
- alias: Light_F0_CR_ToD_LvlAdj2 # adjust light level
  trigger:
    platform: sun
    event: sunrise
    offset: '00:30:00'
  action:
    - service: input_number.set_value
      data:
        entity_id: input_number.light_f0_cr_ctrl
        value: 192
- alias: Light_F0_CR_ToD_LvlAdj3 # adjust light level
  trigger:
    platform: sun
    event: sunset
    offset: '00:30:00'
  action:
    - service: input_number.set_value
      data:
        entity_id: input_number.light_f0_cr_ctrl
        value: 128

The problem now is that - if the set level is say 64, and the previous level the light was on at was say 192 - then the light comes on at 192 then updates to what it should be at 64 (so it flares). This sort of defeats the object of causing minimal disruption at night (and looks really strange too). So, what I thought was to intercept the off event and repeat it but whilst also setting brightness to the minimum level, so :-

- alias: Light_F0_CR_To_Off # light set to min brightness on 'Off' Stops fare if previous setting higher
  trigger:
    platform: state
    entity_id: light.aeotec_dsc19103_micro_smart_dimmer_2nd_edition_level
    to: 'off'
  action:
    service: light.turn_off
    entity_id: light.aeotec_dsc19103_micro_smart_dimmer_2nd_edition_level
    data:
      brightness: 40

But this does not seem to work >:-<<<
would this need a script to a) check if the script was ‘already running’ and ‘exit’ or - switch the light on at a low level, waiting a bit and then rewriting the low level (in case there are transitions) before switching it off finally ???
Another problem I’ve thought of is “what do you do in an emergency ?” or “I need the light on full” - I’m thinking that if I toggle the light switch (on-off-on) so I have 2x"On’s" within 5 seconds then I can set the light to 255, but I have no idea how to capture that - would that be another script ???
I’ve not got as far as scripts yet as I don’t feel I’ve got to ‘bumbling along’ with triggers, conditions and actions yet.
Sorry if this is absorbing your time
Mutt

There is no attribute for brightness when turning off (for obvious reasons) hence my suggestion you briefly turn on the bulb with the new settings and then turn it off, so that it’s ready to use at the intended setting.

When turning on the bulb you do have the brightness setting so can override any previous settings in an emergency.

I would have an emergency input_boolean that you set with something either from an alarm automation or other way (frontend) or script and use that as a condition when turning on the bulb such that it goes to full brightness.

I think your idea is feasible in appdaemon, but would probably be hard in yaml, but maybe someone has already done it and will chime in here :slight_smile:

Keith,
I really appreciate your help here, but I also like the idea that others ‘may’ have different perspectives given their experience and knowledge (lucky b@st@rds ! : - > )
From your latest nugget, I think I’m favoring :-

  1. detecting the switch off
  2. switching back on which will remember the last illumination level
  3. wait a bit (as little as possible)
  4. writing the ‘new’ minimum level to the dimmer
  5. switching the light off (for proper)
    5a. This seems likely to require a script (so it doesn’t get in a constant switch off loop), so I’ll need to start with scripts nowish, before I’ve got even an ‘E’ grade in automations. (and definitely if I were to attempt the ‘emergency full light level’)

AppDaemon seems like 15 steps beyond that to a newbie like me.

Regardless, thanks for your help, though I still welcome any new contributions from yourself or anyone else.
I shall post back here with any progress on this as it will keep it live and will help others doing the same sort of thing and should minimise their learning curve.