Help needed: Check light state, if ON set to a specific kelvin value if OFF do nothing

Hello all,

I am trying to achieve the following via script or scenes but unfortunately I got limited knowledge of HA since I am a new user.
Basically i want a script or something similar that will accomplish the following:

When run, check what lights are state:on and change them to a specific kelvin value. If the light is off then do nothing.

Basically something like this

if light.bedroom_lamp state:off then do nothing, if light.office_lamp state:on set to kelvin 2000 and so on

Do you know if this is possible?

thank you

Hi. You just need to use the condition action in script like I have used here. The condition action checks id the condition is valid and then only proceed with the flow…

alias: Lights Function
sequence:
  - condition: state
    entity_id:  light.living_room_led
    for: 'on'
  - service: light.turn_on
    data:
      color_temp: 2000
    entity_id: light.living_room_led
mode: single

Hey, thanks for the reply. yep pretty close with that.
I have modified it a bit, so my script looks like this

alias: Lights Back to Normal
sequence:
  - condition: state
    entity_id: light.office_lamp
    state: 'on'
  - service: light.turn_on
    data:
      color_temp: 2000
      brightness: 100
    entity_id: light.office_lamp
  - condition: state
    entity_id: light.bedroom_lamp
    state: 'on'
  - service: light.turn_on
    data:
      color_temp: 2000
      brightness: 100
    entity_id: light.bedroom_lamp
  - condition: state
    entity_id: light.island_lamp
    state: 'on'
  - service: light.turn_on
    data:
      color_temp: 2000
      brightness: 100
    entity_id: light.island_lamp
  - condition: state
    entity_id: light.dining_lamp
    state: 'on'
  - service: light.turn_on
    data:
      color_temp: 2000
      brightness: 100
    entity_id: light.dining_lamp
  - condition: state
    entity_id: light.floor_lamp
    state: 'on'
  - service: light.turn_on
    data:
      color_temp: 2000
      brightness: 100
    entity_id: light.floor_lamp
mode: single
icon: 'mdi:lightbulb-multiple-outline'

Maybe there is quicker solution for including all the lights but i did manually with the entity_id. Basically i have included all my HUE lights that are able to change color.

thanks again

did you check the working of this script? This is a sequence meaning that if the first the condition is false then the sequence will not go forward. Like that the sequence will only move till a condition is false.

Are you using nodered?

not working now. when i did my test all the lights were on and on the other test only the office light was on so the script went through.

I have nodered but i am not using it

You could implement this more efficiently in node red. Please import this flow to nodered and see. After importing you need to change the home assistant server to your in nodes and also mqtt server.

[{"id":"746fed5e.e50ef4","type":"mqtt in","z":"51045e07.bbf87","name":"","topic":"light/check","qos":"2","datatype":"auto","broker":"e50ef235.58026","x":220,"y":1280,"wires":[["79fb0ea4.4159b","7a2d5f63.9ed62","c625c91c.0aa2d8","b829b75e.5d6978","1308d089.24c69f"]]},{"id":"79fb0ea4.4159b","type":"api-current-state","z":"51045e07.bbf87","name":"office_lamp","server":"d177950.2a5aa68","version":1,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"light.office_lamp","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":410,"y":1180,"wires":[["3c38c266.62d4ae"],[]]},{"id":"7a2d5f63.9ed62","type":"api-current-state","z":"51045e07.bbf87","name":"bedroom_lamp","server":"d177950.2a5aa68","version":1,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"light.bedroom_lamp","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":420,"y":1220,"wires":[["84587de.bef9c8"],[]]},{"id":"c625c91c.0aa2d8","type":"api-current-state","z":"51045e07.bbf87","name":"island_lamp","server":"d177950.2a5aa68","version":1,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"light.island_lamp","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":410,"y":1260,"wires":[["8678761d.11efe8"],[]]},{"id":"b829b75e.5d6978","type":"api-current-state","z":"51045e07.bbf87","name":"dining_lamp","server":"d177950.2a5aa68","version":1,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"light.dining_lamp","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":410,"y":1300,"wires":[["9e952c21.b2a08"],[]]},{"id":"1308d089.24c69f","type":"api-current-state","z":"51045e07.bbf87","name":"floor_lamp","server":"d177950.2a5aa68","version":1,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","override_topic":false,"entity_id":"light.floor_lamp","state_type":"str","state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","blockInputOverrides":false,"x":410,"y":1340,"wires":[["2f78635.32a929c"],[]]},{"id":"3c38c266.62d4ae","type":"api-call-service","z":"51045e07.bbf87","name":"office_lamp","server":"d177950.2a5aa68","version":1,"debugenabled":false,"service_domain":"light","service":"turn_on","entityId":"light.office_lamp","data":"{\"color_temp\":\"2000\",\"brightness\":\"100\"}","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":610,"y":1180,"wires":[[]]},{"id":"84587de.bef9c8","type":"api-call-service","z":"51045e07.bbf87","name":"bedroom_lamp","server":"d177950.2a5aa68","version":1,"debugenabled":false,"service_domain":"light","service":"turn_on","entityId":"light.bedroom_lamp","data":"{\"color_temp\":\"2000\",\"brightness\":\"100\"}","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":620,"y":1220,"wires":[[]]},{"id":"8678761d.11efe8","type":"api-call-service","z":"51045e07.bbf87","name":"island_lamp","server":"d177950.2a5aa68","version":1,"debugenabled":false,"service_domain":"light","service":"turn_on","entityId":"light.island_lamp","data":"{\"color_temp\":\"2000\",\"brightness\":\"100\"}","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":610,"y":1260,"wires":[[]]},{"id":"9e952c21.b2a08","type":"api-call-service","z":"51045e07.bbf87","name":"dining_lamp","server":"d177950.2a5aa68","version":1,"debugenabled":false,"service_domain":"light","service":"turn_on","entityId":"light.dining_lamp","data":"{\"color_temp\":\"2000\",\"brightness\":\"100\"}","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":610,"y":1300,"wires":[[]]},{"id":"2f78635.32a929c","type":"api-call-service","z":"51045e07.bbf87","name":"floor_lamp","server":"d177950.2a5aa68","version":1,"debugenabled":false,"service_domain":"light","service":"turn_on","entityId":"light.floor_lamp","data":"{\"color_temp\":\"2000\",\"brightness\":\"100\"}","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":610,"y":1340,"wires":[[]]},{"id":"4fde12cc.bbbbdc","type":"inject","z":"51045e07.bbf87","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":220,"y":1240,"wires":[["79fb0ea4.4159b","7a2d5f63.9ed62","c625c91c.0aa2d8","b829b75e.5d6978","1308d089.24c69f"]]},{"id":"e50ef235.58026","type":"mqtt-broker","name":"Cloudmqtt","broker":"hairdresser.cloudmqtt.com","port":"17088","clientid":"","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"d177950.2a5aa68","type":"server","name":"Home Assistant","legacy":false,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true}]

If you need any help with nodered, let me know

wow thanks. yeah it get a bit complicated now because i dont have MQTT broker. as i have seen on the HA docs i need something like CloudMQTT which has a recurring fee and a public broker like Mosquitto is not recommended for running home automatons due to the fact that is a public broker

Please use mosquito mqtt. Its there in the HA addon store. CloudMQTT is something that i experimented with. You need to go to the HA addon store and install mosquitto mqtt then start the addon. Then check logs if it has started. You can now add an mqtt server to node red.

To call this service you can use other methods also, like making a input_boolean and using it with node red to call the service.

Before proceeding with MQTT solution and node red i did one last “cooking” With a script running other scripts and it worked. well i did 3 tests and were ok. but i am not sure if i am missing something.

scripts syntax:

alias: Lights Back to Normal FLOOR LAMP
sequence:
  - condition: state
    entity_id: light.floor_lamp
    state: 'on'
  - service: light.turn_on
    data:
      color_temp: 2000
      brightness: 100
    entity_id: light.floor_lamp
mode: single
icon: 'mdi:lightbulb-multiple-outline'

same syntax for all of them. Changing entity_id accordingly

script running the scripts

alias: Lights to 2000K
sequence:
  - service: script.lights_back_to_normal_floor_lamp
    data: {}
  - service: script.lights_back_to_normal_kitchen
    data: {}
  - service: script.lights_back_to_normal_bedroom
    data: {}
  - service: script.lights_back_to_normal_dining
    data: {}
  - service: script.lights_back_to_normal_office
    data: {}
mode: single

Yes this will work. This is adapting the first script to rest of the devices and combining them.

yep. so far it does. thanks for the help and the guidance!

do you think that scripts within script is a bad practice and can lead to other issues? or as long it works is fine?

Script within script is not a bad idea. This will work and if anything, we can deal with it then