Bed side lamp and temperature

I would like to create an automation that allows me to turn on a light and to change the color of the lamp depending on the temperature detected outside by a netatmo sensor but it seems that it does not fade. The conditions are:

  • the lamp must light up if I’m inside the house
    at a certain time if it is a holiday or weekday and in addition to change color depending on the temperature
    Thank you
    this is the code:
- id: '254636889'
  alias: Bed Side Lamp Freddissimo Alba
  trigger:
  - at: 07:30:00
    platform: time
  condition:
  - condition: state
    entity_id: device_tracker.iphone_matteo
    state: home
  - condition: state
    entity_id: binary_sensor.workday_sensor
    state: 'on'
  - above: '10'
    below: '0'
    condition: numeric_state
    entity_id: sensor.netatmo_matteo_via_udine_temperature
  action:
  - data:
      brightness: 120
      color_temp: 435
      entity_id: light.bed_side_lamp
      rgb_color:
      - 182
      - 255
      - 255
    service: light.turn_on
  - delay: 0:10
  - data:
      entity_id: light.bed_side_lamp
    service: light.turn_off
- id: '2454636889'
  alias: Bed Side Lamp Freddissimo Tramonto
  trigger:
  - event: sunset
    offset: +00:00:00
    platform: sun
  condition:
  - condition: state
    entity_id: device_tracker.iphone_matteo
    state: home
  - above: '10'
    below: '0'
    condition: numeric_state
    entity_id: sensor.netatmo_matteo_via_udine_temperature
  action:
  - data:
      brightness: 120
      color_temp: 435
      entity_id: light.bed_side_lamp
      rgb_color:
      - 182
      - 255
      - 255
    service: light.turn_on
  - delay: 0:10
  - data:
      entity_id: light.bed_side_lamp
    service: light.turn_off
- id: '254786889'
  alias: Bed Side Lamp Freddo Alba
  trigger:
  - at: 07:30:00
    platform: time
  condition:
  - condition: state
    entity_id: device_tracker.iphone_matteo
    state: home
  - condition: state
    entity_id: binary_sensor.workday_sensor
    state: 'on'
  - above: '20'
    below: '10'
    condition: numeric_state
    entity_id: sensor.netatmo_matteo_via_udine_temperature
  action:
  - data:
      brightness: 120
      color_temp: 435
      entity_id: light.bed_side_lamp
      rgb_color:
      - 255
      - 255
      - 145
    service: light.turn_on
  - delay: 0:10
  - data:
      entity_id: light.bed_side_lamp
    service: light.turn_off
- id: '24455689'
  alias: Bed Side Lamp Freddo Tramonto
  trigger:
  - event: sunset
    offset: +00:00:00
    platform: sun
  condition:
  - condition: state
    entity_id: device_tracker.iphone_matteo
    state: home
  - above: '20'
    below: '10'
    condition: numeric_state
    entity_id: sensor.netatmo_matteo_via_udine_temperature
  action:
  - data:
      brightness: 120
      color_temp: 435
      entity_id: light.bed_side_lamp
      rgb_color:
      - 255
      - 255
      - 145
    service: light.turn_on
  - delay: 0:10
  - data:
      entity_id: light.bed_side_lamp
    service: light.turn_off
- id: '2656586889'
  alias: Bed Side Lamp Caldo Alba
  trigger:
  - at: 07:30:00
    platform: time
  condition:
  - condition: state
    entity_id: device_tracker.iphone_matteo
    state: home
  - condition: state
    entity_id: binary_sensor.workday_sensor
    state: 'on'
  - above: '30'
    below: '20'
    condition: numeric_state
    entity_id: sensor.netatmo_matteo_via_udine_temperature
  action:
  - data:
      brightness: 120
      color_temp: 435
      entity_id: light.bed_side_lamp
      rgb_color:
      - 255
      - 108
      - 145
    service: light.turn_on
  - delay: 0:10
  - data:
      entity_id: light.bed_side_lamp
    service: light.turn_off
- id: '244689589'
  alias: Bed Side Lamp Caldo Tramonto
  trigger:
  - event: sunset
    offset: +00:00:00
    platform: sun
  condition:
  - condition: state
    entity_id: device_tracker.iphone_matteo
    state: home
  - above: '30'
    below: '20'
    condition: numeric_state
    entity_id: sensor.netatmo_matteo_via_udine_temperature
  action:
  - data:
      brightness: 120
      color_temp: 435
      entity_id: light.bed_side_lamp
      rgb_color:
      - 255
      - 108
      - 145
    service: light.turn_on
  - delay: 0:10
  - data:
      entity_id: light.bed_side_lamp
    service: light.turn_off
- id: '2675586889'
  alias: Bed Side Lamp Caldissimo Alba
  trigger:
  - at: 07:30:00
    platform: time
  condition:
  - condition: state
    entity_id: device_tracker.iphone_matteo
    state: home
  - condition: state
    entity_id: binary_sensor.workday_sensor
    state: 'on'
  - above: '40'
    below: '30'
    condition: numeric_state
    entity_id: sensor.netatmo_matteo_via_udine_temperature
  action:
  - data:
      brightness: 120
      color_temp: 435
      entity_id: light.bed_side_lamp
      rgb_color:
      - 255
      - 0
      - 63
    service: light.turn_on
  - delay: 0:10
  - data:
      entity_id: light.bed_side_lamp
    service: light.turn_off
- id: '249889589'
  alias: Bed Side Lamp Caldissimo Tramonto
  trigger:
  - event: sunset
    offset: +00:00:00
    platform: sun
  condition:
  - condition: state
    entity_id: device_tracker.iphone_matteo
    state: home
  - above: '40'
    below: '30'
    condition: numeric_state
    entity_id: sensor.netatmo_matteo_via_udine_temperature
  action:
  - data:
      brightness: 120
      color_temp: 435
      entity_id: light.bed_side_lamp
      rgb_color:
      - 255
      - 0
      - 63
    service: light.turn_on
  - delay: 0:10
  - data:
      entity_id: light.bed_side_lamp
    service: light.turn_off
- id: '123456'
  alias: Bed Side Lamp Freddissimo Alba festivo
  trigger:
  - at: 09:30:00
    platform: time
  condition:
  - condition: state
    entity_id: device_tracker.iphone_matteo
    state: home
  - condition: state
    entity_id: binary_sensor.workday_sensor
    state: of
  - above: '10'
    below: '0'
    condition: numeric_state
    entity_id: sensor.netatmo_matteo_via_udine_temperature
  action:
  - data:
      brightness: 120
      color_temp: 435
      entity_id: light.bed_side_lamp
      rgb_color:
      - 182
      - 255
      - 255
    service: light.turn_on
  - delay: 0:10
  - data:
      entity_id: light.bed_side_lamp
    service: light.turn_off
- id: '2547866987'
  alias: Bed Side Lamp Freddo Alba Festivo
  trigger:
  - at: 09:30:00
    platform: time
  condition:
  - condition: state
    entity_id: device_tracker.iphone_matteo
    state: home
  - condition: state
    entity_id: binary_sensor.workday_sensor
    state: 'off'
  - above: '20'
    below: '10'
    condition: numeric_state
    entity_id: sensor.netatmo_matteo_via_udine_temperature
  action:
  - data:
      brightness: 120
      color_temp: 435
      entity_id: light.bed_side_lamp
      rgb_color:
      - 255
      - 255
      - 145
    service: light.turn_on
  - delay: 0:10
  - data:
      entity_id: light.bed_side_lamp
    service: light.turn_off
- id: '26565745689'
  alias: Bed Side Lamp Caldo Alba festivo
  trigger:
  - at: 09:30:00
    platform: time
  condition:
  - condition: state
    entity_id: device_tracker.iphone_matteo
    state: home
  - condition: state
    entity_id: binary_sensor.workday_sensor
    state: 'off'
  - above: '30'
    below: '20'
    condition: numeric_state
    entity_id: sensor.netatmo_matteo_via_udine_temperature
  action:
  - data:
      brightness: 120
      color_temp: 435
      entity_id: light.bed_side_lamp
      rgb_color:
      - 255
      - 108
      - 145
    service: light.turn_on
  - delay: 0:10
  - data:
      entity_id: light.bed_side_lamp
    service: light.turn_off
- id: '234325889'
  alias: Bed Side Lamp Caldissimo Alba festivo
  trigger:
  - at: 09:30:00
    platform: time
  condition:
  - condition: state
    entity_id: device_tracker.iphone_matteo
    state: home
  - condition: state
    entity_id: binary_sensor.workday_sensor
    state: 'off'
  - above: '40'
    below: '30'
    condition: numeric_state
    entity_id: sensor.netatmo_matteo_via_udine_temperature
  action:
  - data:
      brightness: 120
      color_temp: 435
      entity_id: light.bed_side_lamp
      rgb_color:
      - 255
      - 0
      - 63
    service: light.turn_on
  - delay: 0:10
  - data:
      entity_id: light.bed_side_lamp
    service: light.turn_off

I haven’t gone through the entire thing, but right off the bat I can see two issues.

First, you have numeric_state conditions where you specify both above and below, but given the values you specify it can never be true. E.g., you have sensor.netatmo_matteo_via_udine_temperature must be above 10 AND below 0. That can’t be.

Second, you’re calling the light.turn_on service while specifying both color_temp and rgb_color. You can’t use two color specifications in the same service call. Use color_temp or rgb_color, but not both. I’m assuming you must be getting errors. Check the log.

how can I turn the bed_side light on if the temperature is between 10 and 20 degrees detected by the netatmo sensor and only if I am at home?

You need to think in terms of events and conditions. Triggers define events, and conditions define, well, conditions. The triggers/events control when an automation’s actions will run, and the conditions control if the actions will run when one of the trigger events occurs. So, what event or events do you want to cause the actions to run, and what condition or conditions do you want to decide if the actions will run (when one of the triggers occurs)?

the event is that if at 7.30 am, if I am at home, if it is a working day, if the temperature is in a certain range, bed side turns on and turns into a certain color. After 10 minutes the bed side lamp turns off.
if it is a non-working day, the switch-on time will be at 9.30 am, however following all these conditions.

this is new configuration only for one section

- id: '254636889'
  alias: Bed Side Lamp Freddissimo Alba
  trigger:
  - at: 07:30:00
    platform: time
  condition:
  - condition: state
    entity_id: device_tracker.iphone_matteo
    state: home
  - condition: state
    entity_id: binary_sensor.workday_sensor
    state: 'on'
  - above: '0'
    below: '10'
    condition: numeric_state
    entity_id: sensor.netatmo_matteo_via_udine_temperature
  action:
  - data:
      brightness: 120
      entity_id: light.bed_side_lamp
      rgb_color:
      - 182
      - 255
      - 255
    service: light.turn_on
  - delay: 0:10
  - data:
      entity_id: light.bed_side_lamp
    service: light.turn_off

No, that is a combination of events and conditions. The event is time becoming 7:30 am. The rest are conditions.

Events are when something changes. E.g., time changes to 7:30 am, not time is 7:30 am. Or you come home (i.e., device_tracker changes to home from some other state), not you are home.

Conditions are when something is in a particular state.

It’s an important distinction since you write triggers to capture events, whereas conditions test the current state of things when the trigger/event happens.

Yes, this looks like it will do what you want for the working day scenario. For the non-working day scenario, you could copy it and change the trigger time and workday condition. However, you can do both scenarios in one automation like this:

- id: '254636889'
  alias: Bed Side Lamp Freddissimo Alba
  trigger:
  - at: 07:30:00
    platform: time
  - at: 09:30:00
    platform: time
  condition:
  - condition: state
    entity_id: device_tracker.iphone_matteo
    state: home
  - condition: template
    value_template: >
      {{ is_state('binary_sensor.workday_sensor', 'on') and now().hour == 7 or
         is_state('binary_sensor.workday_sensor', 'off') and now().hour == 9 }}
  - above: '0'
    below: '10'
    condition: numeric_state
    entity_id: sensor.netatmo_matteo_via_udine_temperature
  action:
  - data:
      brightness: 120
      entity_id: light.bed_side_lamp
      rgb_color:
      - 182
      - 255
      - 255
    service: light.turn_on
  - delay: 0:10
  - data:
      entity_id: light.bed_side_lamp
    service: light.turn_off

if I wanted to use the 4 hypotheses in a single automation, very cold, cold, hot, very hot, with different lamp colors, subdividing into holidays and weekdays, then as a 7.30 am holiday and 9.30 am as a weekday, and more into the sunset at four a very cold hypothesis, cold, hot, very hot, with the state of presence omitting only the holiday and the weekdays, would it be possible? It was what I wrote in the first post …

most of the time, anything is possible…

start small and build on that, and keep it working :wink:

if I were you, Id rewrite the automation to contain the main logic, use a few intermediary script with some extra conditions, and logic, and finally call scenes, to do the specific lighting settings.

that way you get a maintainable system, and should yo decide to add or changes settings, conditions, logic, you only have to edit the one piece of code.

Just in the other automation you (we) are working on.

the first post is such a long automation, it might be very hard to maintain, and track errors.

this is already hard to understand in plain english, let alone translate it to an automation…cut it down to logical building blocks, separate these, and bind them together in a main structure. Architecture is important, use (reusable) building blocks.

- id: '254636889'
  alias: Bed Side Lamp Freddissimo Alba
  trigger:
  - at: 07:30:00
    platform: time
  - at: 09:30:00
    platform: time
  condition:
  - condition: state
    entity_id: device_tracker.iphone_matteo
    state: home
  - condition: template
    value_template: >
      {{ is_state('binary_sensor.workday_sensor', 'on') and now().hour == 7 or
         is_state('binary_sensor.workday_sensor', 'off') and now().hour == 9 }}
  action:
  - data_template:
      brightness: 120
      entity_id: light.bed_side_lamp
      color_name: >
        {% set temp = states('sensor.netatmo_matteo_via_udine_temperature')|float %}
        {% if temp < 10 %}
          blue
        {% elif temp < 20 %}
          lightblue
        {% elif temp < 30 %}
          pink
        {% else %}
          red
        {% endif %}
    service: light.turn_on
  - delay: 0:10
  - data:
      entity_id: light.bed_side_lamp
    service: light.turn_off

Of course change the threshold values and colors as you desire. And I might have gotten the time vs workday backwards (it was kind of hard to tell which you wanted), so change that, too, if it’s wrong.

Though I don’t want to interfere in any way with @pnbruckner 's sublime codecs, as the above proves again, for me it has helped me enormously to be rigid in the way to write automations. It helps me to debug things on sight, and see which pieces might be missing. Also, I do like to have clean and tight looking code with rhythmic indents (musician here, of the percussive kind…)

my version of this would be:

- alias: 'Bed Side Lamp Freddissimo Alba'
  id: 'Bed Side Lamp Freddissimo Alba'
  trigger:
    - platform: time #indent 2 spaces after each automation section trigger, condition action, and always start with the platform
      at: 07:30:00
    - platform: time
      at: 09:30:00
  condition:
    - condition: state #again, indent 2 spaces, start with the 'platform' , then entity_id, than states or attributes
      entity_id: device_tracker.iphone_matteo
      state: home
    - condition: template
      value_template: > # always use multiline, saves on (mistakes with) quotes. A lot.
        {{ is_state('binary_sensor.workday_sensor', 'on') and now().hour == 7 or
           is_state('binary_sensor.workday_sensor', 'off') and now().hour == 9 }}
        # always try to write conditions with states(), or is(state) versus state.entity_id.state
  action:
    - service: light.turn_on #also in the action part, start with the service
      entity_id: light.bed_side_lamp #then entity_id
      data_template: #then states and/or attributes
        brightness: 120
        color_name: >
          {% set temp = states('sensor.netatmo_matteo_via_udine_temperature')|float %}
          {% if temp < 10 %} blue
          {% elif temp < 20 %} lightblue
          {% elif temp < 30 %} pink
          {% else %} red
          {% endif %}
           #writing the if, then else like this, immediately shows if a section is missing or not
    - delay: 
        minutes: 10 #there have been reports of issues with the other notation, this never fails
    - service: light.turn_off
      entity_id: light.bed_side_lamp #don't use the data field if no extra data is required. 

Hope this helps, it certainly does it for me.
One might even consider the action bit, and write that as a script. Call the script in the automation, might use a variable to pass to the script, and (re-) use the script in other automations.

sorry for my english

about the colors mentioned which code I have to enter:
these?
brightness: 120
color_temp: 435
entity_id: light.bed_side_lamp
rgb_color:
- 182
- 255
- 255

no no that’s not al all what I meant! Sorry if that came across as a negative comment. Wasn’t my intention at all.

What I meant was that you have a lot of complex conditions and triggers you want to combine in 1 large automation.
My suggestion was merely to take it step by step, and build and work with a framework you can expand on.

not sure I follow, this is not in the automation above is it?
also, that combination won’t work. You either use rgb_color, or color_temp. They won’t go together, please read up on this subject, it can be confusing, but that is the mean rule…

You should check for each light which attributes they support, do the in the dev-state:

you will see that when hs_color, or one of the other _color attributes is available, the color_temp isn’t, and vice versa.

Complicating factor will be that, as I already mentioned, people seem to have issues with tradfri lights setting color and brightness in 1 service. You simply will have to experiment.

As for the rgb_colors, you’d best use a scene for that, or use color names (see @pnbruckner post below, or check here Color Names — HTML Color Codes ) in the script directly, colors are difficult to template.

scene:
  - name: Uit huis on
    entities:
      light.kist:
        state: on
        transition: 4
        brightness: 173
        hs_color: [36,66]

real life example:

in my ‘Work’ scenario this script is called:

lighting_aan_de_slag:
  alias: Lighting Aan de slag
  sequence:
    - service: input_select.select_option
      data:
        entity_id: input_select.activity_lighting
        option: Aan de slag
    - service: scene.turn_on # kill lights that can be Off unconditionally
      entity_id: scene.aan_de_slag_off
    - condition: template
      value_template: > #test if light_level is ok, if not turn on extra lights
        {{ is_state('binary_sensor.control_room_lux_input', 'on')}}
    - service: scene.turn_on
      entity_id: scene.aan_de_slag_on

one of the scenes:

- name: Aan de slag on
  entities:
    light.bureau_left:
      state: on
      transition: 4
      brightness: 233
      color_temp: 370
    light.bureau_right:
      state: on
      transition: 4
      brightness: 233
      color_temp: 370
    light.office_inside:
      state: on
      transition: 4
      brightness: 127
    light.office_outside:
      state: on
      transition: 4
      brightness: 127
      color_temp: 397

doing it like this might seem a bit overwhelming at first. Once you get the hang of that, you can adjust to your liking where ever you want, without having to change the architecture of the main logic.

The rgb_color parameter requires a sequence of three numbers, and that’s really not very easy to do with Jinja templates. It’s much easier to use a color name. You can find a list of color names (and their RGB equivalents) that you can use here.

would it be possible to insert also the sunset scenario in this automation independent of holidays and weekdays?
When I go to enter the color specifications for the lamp, can I write the name of the colors directly?
Sorry again but this is the first time I have tried such complex automation.

this is the automation I wrote but it still doesn’t seem to work…

- alias: 'Bed Side Lamp alba'
  id: Bed Side Lamp alba
  trigger:
    - platform: time #indent 2 spaces after each automation section trigger, condition action, and always start with the platform
      at: 07:30:00
    - platform: time
      at: 09:30:00
  condition:
    - condition: zone
      entity_id: device_tracker.iphone_matteo
      zone: zone.home
    - condition: template
      value_template: > # always use multiline, saves on (mistakes with) quotes. A lot.
        {{ is_state('binary_sensor.workday_sensor', 'on') and now().hour == 7 or
           is_state('binary_sensor.workday_sensor', 'off') and now().hour == 9 }}
        # always try to write conditions with states(), or is(state) versus state.entity_id.state
  action:
    - service: light.turn_on #also in the action part, start with the service
      entity_id: light.bed_side_lamp #then entity_id
      data_template: #then states and/or attributes
        brightness: 120
        color_name: >
          {% set temp = states('sensor.netatmo_matteo_via_udine_temperature')|float %}
          {% if temp < 10 %} blue
          {% elif temp < 20 %} lightblue
          {% elif temp < 30 %} pink
          {% else %} red
          {% endif %}
           #writing the if, then else like this, immediately shows if a section is missing or not
    - delay: 
        minutes: 10 #there have been reports of issues with the other notation, this never fails
    - service: light.turn_off
      entity_id: light.bed_side_lamp #don't use the data field if no extra data is required.

please explain what exactly doesnt work?

trigger, condition, action? does the color_name template work in dev-template …?

the lights do not come on at that time