Complete newbie: trying to turn the light scene depending on the room temp

Hi,
I’m trying to get the lights in the livingroom to warn us (by turning blue) if the temperature upstairs gets below 17C
YAML is more than a foreign language to me, I have zero programming skills/knowledge.

So by using the UI I’ve managed to get my Hue lights to change blue when the Hue motion/temp sensor goes below 17C AND the livingroom lights are already on.

alias: Upstairs temp ALARM below 17
description: ""
trigger:
  - platform: numeric_state
    entity_id: sensor.hue_motion_sensor_2_temperature
    above: 0
    below: 17
condition:
  - condition: state
    entity_id: light.livingroom_rear_ceiling
    state: "on"
action:
  - service: scene.turn_on
    target:
      entity_id: scene.blue_livingroom
    metadata: {}
mode: single

The problem I have is that I can’t work out how to have the automation then change back to the original scene as the heating raises back above 17C AND the rear ceiling light is still on.
Basically that rear light won’t be on if we are not at home or have gone to bed so I’m using it as the “condition”.

I hope this makes sense and that my pasted code is set out correctly.

I’m using;
Rpi 4 running Home Assistant 2022.12.8
Supervisor 2022.12.1
Operating System 9.4
Frontend 20221213.1 - latest

Hue hub
Hue lights
Hue motion/temp sensors

Oh and having to do it all through the Home Assistant app on iPhone.

Thanks for any input.

Are you trying to this all in one automation? The simpler idea would be to create a seperate automation that triggers on above: 17

Just remember that trigger below, above etc only work when they cross that value, thus if you have an automation that triggers for above 17 degrees and the condition fails, it won’t trigger again until the temp goes below 17 then above again.

Ah, so you are saying I should make many simple automations instead of a few complicated ones? I guess that makes sense but how can I get HA to know what the scene was before it turned blue, you know so I can get it to change back to that scene after it raises above 17C?

Hmm it’s interesting about the trigger. I guess this means if the temp is already below 17C and then I come home and switch the livingroom lights on, it won’t trigger the blue scene as it hasn’t crossed the 17. How could I fix this issue?
I guess I’m looking for some sort of code that asks what the temp is every 10 mins and then use that as a trigger?

Really appreciate the input Daryl.

Your trigger then is turning the switch on, and the action (turning the light on and setting the light colour) depends on a condition (the temp).

When you run the automation to change the colour, you can save the current scene to a temporary one and then restore the temporary one at the right time. Use the scene.create service.

1 Like

I would put it all in one automation and use trigger id’s.

3 triggers: temp below 17, temp above 17 and light state changes to on.
Give them all unique trigger id’s and set up several different “choose” options as actions with the condition “triggered by”.

The first option for example should have condition “triggered by” temp-below-17 and state light on. Then the action to create the current scene and set the blue scene.
Second option similar to the first one but action to set the scene to the preciously created scene.
Then you need a final option that creates the scene and sets the blue scene if triggered by light turn on.
Let me know if you need a code example.

Edit: I couldn´t sleep :wink:

alias: Upstairs temp ALARM
description: ""
mode: single
trigger:
  - platform: numeric_state
    entity_id: sensor.hue_motion_sensor_2_temperature
    below: 17
    id: temp-low
  - platform: numeric_state
    entity_id: sensor.hue_motion_sensor_2_temperature
    above: 17
    id: temp-high
  - platform: state
    entity_id:
      - light.livingroom_rear_ceiling
    to: "on"
    id: light-on
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id: temp-low
          - condition: state
            entity_id: light.livingroom_rear_ceiling
            state: "on"
        sequence:
          - service: scene.create
            data:
              scene_id: light_before_cold
              snapshot_entities: light.livingroom_rear_ceiling
          - service: scene.turn_on
            target:
              entity_id: scene.blue_livingroom
            metadata: {}
      - conditions:
          - condition: trigger
            id: temp-high
          - condition: state
            entity_id: light.livingroom_rear_ceiling
            state: "on"
        sequence:
          - service: scene.turn_on
            target:
              entity_id: scene.light_before_cold
            metadata: {}
      - conditions:
          - condition: trigger
            id: light-on
          - condition: numeric_state
            entity_id: sensor.hue_motion_sensor_2_temperature
            below: 17
        sequence:
          - service: scene.create
            data:
              scene_id: light_before_cold
              snapshot_entities: light.livingroom_rear_ceiling
          - service: scene.turn_on
            target:
              entity_id: scene.blue_livingroom
            metadata: {}

Only problem I see here is that nothing will happen if you turn off the light while it´s blue, and meanwhile the temperature goes above 17. Then you turn on the light, it will still be blue.
This can of course be solved with another option if needed.

2 Likes

LOL well done :slight_smile:

1 Like

Wow, thank you Rodak, I have just spent the morning putting the sensor in the fridge and then under my armpit to get it to cycle through the temp ranges so I could test it :smiley:
This is pretty much what I wanted plus I think I can actually (in a newbie way) understand how the code is working.
Again thank you so much.

Glad I could help. Trigger id’s are awesome and I use them all the time to bake many related automations into a single one.
To be honest I don’t understand why the devs decided to hide them behind the 3-dot menu for every trigger. Should be fully visible so people can use them in plain sight.
Also to spare your fridge from your armpit or your armpit from the cold sensor, you can head over to developer tools, then states and set the state manually for your sensor for testing.
The next real sensor update will override your manual input so you don’t have to worry that something will break.

Do you need the option for the scenario when you turn off the light while cold and then the temperature rises above 17?

1 Like

Thanks Rodak, I’m not sure what you mean tho.

The problem I have now is I’ve tried to update the code so it’s now time dependent as well. I’ve realised I have the lights on early, damn winter, and I don’t need the alarm till later on. So I’ve tried to make it work between 18:30-5:00 every day.

alias: Upstairs temp ALARM automation
description: ""
trigger:
  - platform: numeric_state
    entity_id: sensor.hue_motion_sensor_2_temperature
    below: 17
    id: temp-low
  - platform: numeric_state
    entity_id: sensor.hue_motion_sensor_2_temperature
    above: 17
    id: temp-high
  - platform: state
    entity_id:
      - light.livingroom_rear_ceiling
    to: "on"
    id: light-on
condition: []
action:
  - choose:
      - conditions:
         - condition: time
           after: "18:30:00"
           before: "05:00:00"
           weekday:
             - mon
             - tue
             - wed
             - thu
             - fri
             - sat
             - sun
      - conditions:
          - condition: trigger
            id: temp-low
          - condition: state
            entity_id: light.livingroom_rear_ceiling
            state: "on"
        sequence:
          - service: scene.create
            data:
              scene_id: light_before_cold
              snapshot_entities: light.livingroom_rear_ceiling
          - service: scene.turn_on
            target:
              entity_id: scene.blue_livingroom
            metadata: {}
      - conditions:
          - condition: trigger
            id: temp-high
          - condition: state
            entity_id: light.livingroom_rear_ceiling
            state: "on"
        sequence:
          - service: scene.turn_on
            target:
              entity_id: scene.light_before_cold
            metadata: {}
      - conditions:
          - condition: trigger
            id: light-on
          - condition: numeric_state
            entity_id: sensor.hue_motion_sensor_2_temperature
            below: 17
        sequence:
          - service: scene.create
            data:
              scene_id: light_before_cold
              snapshot_entities: light.livingroom_rear_ceiling
          - service: scene.turn_on
            target:
              entity_id: scene.blue_livingroom
            metadata: {}
mode: single

As I mentioned on my original post I’m having to do all this with my iPhone. I think HA doesn’t like my spaces as I don’t believe I can tab to get the formatting correct. Plus I think my added code may be rubbish.

You are right, you cannot use tab in yaml.

So I just press space bar to get the formatting?
Thanks