claytonjn / hass-circadian_lighting

Circadian Lighting custom component for Home Assistant
Apache License 2.0
774 stars 89 forks source link

Get brightness without switches #224

Closed thargy closed 1 year ago

thargy commented 1 year ago

Hi,

I've not configured any switches as I don't want to use that functionality. Is there any way to get a brightness value without a switch? There doesn't seem to be a way to create a switch without specifying some lights to control.

anthortic commented 1 year ago

I use node red and a function node that takes the sun data.state and converts it to a brightness value where 255 = 100% to achieve what you are looking for. Hope it helps!

var R_colour = msg.data.attributes.rgb_color[0] var G_colour = msg.data.attributes.rgb_color[1] var B_colour = msg.data.attributes.rgb_color[2] var Colour = [R_colour, G_colour, B_colour]

var Convert = Math.round(255 * (msg.data.state / 100))

if (Convert < 25) {
   msg.brightness = 25
} else {
   msg.brightness = Convert
}

var Brightness = msg.brightness

return msg = { "entity_id": msg.entity_id, "execute": msg.execute, "payload": { "data": { "brightness": Brightness, "rgb_color": Colour } } }

anthortic commented 1 year ago

I actually had a bit of a tinker after my previous post and and made my flow more efficient in node red, you are welcome to see for yourself:

[{"id":"352b716a1aaafbd2","type":"server-state-changed","z":"1480002f.f9315","name":"circadian","server":"d9f720b4.8199a","version":4,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"sensor.circadian_values","entityidfiltertype":"exact","outputinitially":true,"state_type":"str","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":false,"for":"0","forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":true,"ignoreCurrentStateUnavailable":true,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"eventData"},{"property":"topic","propertyType":"msg","value":"","valueType":"triggerId"}],"x":80,"y":760,"wires":[["32bb56d6e88e6779"]]},{"id":"39dab8ac3a091499","type":"api-call-service","z":"1480002f.f9315","name":"light adjust","server":"d9f720b4.8199a","version":5,"debugenabled":false,"domain":"light","service":"turn_on","areaId":[],"deviceId":["e1b246c3fa174416bf348a6d11eac72f"],"entityId":[],"data":"","dataType":"json","mergeContext":"","mustacheAltTags":false,"outputProperties":[{"property":"light","propertyType":"msg","value":"light.spare_light_candle_ikea","valueType":"str"}],"queue":"none","x":690,"y":860,"wires":[["12a24aa25c3bdc34"]]},{"id":"bc6a8b879a1eb5e3","type":"api-current-state","z":"1480002f.f9315","name":"light check","server":"d9f720b4.8199a","version":3,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","entity_id":"light.spare_light_candle_ikea","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"}],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":330,"y":860,"wires":[["21239eb49bee434b"],[]]},{"id":"32bb56d6e88e6779","type":"function","z":"1480002f.f9315","name":"function 29","func":"var R_colour = Math.round(msg.data.new_state.attributes.rgb_color[0])\nvar G_colour = Math.round(msg.data.new_state.attributes.rgb_color[1])\nvar B_colour = Math.round(msg.data.new_state.attributes.rgb_color[2])\nvar Colour = [R_colour, G_colour, B_colour]\n\nvar Convert = Math.round(255 * (msg.payload / 100))\n\n if (Convert < 25) {\n msg.brightness = 25\n } else {\n msg.brightness = Convert\n }\n\nvar Brightness = msg.brightness\n\nreturn msg =\n{\n \"payload\": {\n \"data\": {\n \"brightness\": Brightness,\n \"rgb_color\": Colour\n }\n }\n}","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":250,"y":760,"wires":[["b86b91bc007b3a4b","12a24aa25c3bdc34"]]},{"id":"2c11a7d2f546cf60","type":"comment","z":"1480002f.f9315","name":"light - living room candle","info":"","x":370,"y":820,"wires":[]},{"id":"15e61eef80b989b6","type":"comment","z":"1480002f.f9315","name":"light - bed south lamp","info":"","x":360,"y":920,"wires":[]},{"id":"2ddd4f1f7b903168","type":"api-call-service","z":"1480002f.f9315","name":"light adjust","server":"d9f720b4.8199a","version":5,"debugenabled":false,"domain":"light","service":"turn_on","areaId":[],"deviceId":["9358776244e65a93e3a2a9ca71c9e3ff"],"entityId":[],"data":"","dataType":"json","mergeContext":"","mustacheAltTags":false,"outputProperties":[{"property":"light","propertyType":"msg","value":"light.bedroom_south_light_lamp","valueType":"str"}],"queue":"none","x":690,"y":960,"wires":[["12a24aa25c3bdc34"]]},{"id":"b3feb32bf366dd9a","type":"api-current-state","z":"1480002f.f9315","name":"light check","server":"d9f720b4.8199a","version":3,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","entity_id":"light.bedroom_south_light_lamp","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"}],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":330,"y":960,"wires":[["b5f70e55981de9fe"],[]]},{"id":"e5d090b4a624b4a9","type":"comment","z":"1480002f.f9315","name":"light - kitchen ceiling","info":"","x":350,"y":1020,"wires":[]},{"id":"ace20c88462ce178","type":"api-call-service","z":"1480002f.f9315","name":"light adjust","server":"d9f720b4.8199a","version":5,"debugenabled":false,"domain":"light","service":"turn_on","areaId":[],"deviceId":["65d9a54ffa4941a48b0771afbf3cf3b2"],"entityId":[],"data":"","dataType":"json","mergeContext":"","mustacheAltTags":false,"outputProperties":[{"property":"light","propertyType":"msg","value":"light.kitchen_light_ceiling","valueType":"str"}],"queue":"none","x":690,"y":1060,"wires":[["12a24aa25c3bdc34"]]},{"id":"e0d2769bddf89db8","type":"api-current-state","z":"1480002f.f9315","name":"light check","server":"d9f720b4.8199a","version":3,"outputs":2,"halt_if":"on","halt_if_type":"str","halt_if_compare":"is","entity_id":"switch.living_room_switch_light_right","state_type":"str","blockInputOverrides":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"entity"}],"for":"0","forType":"num","forUnits":"minutes","override_topic":false,"state_location":"payload","override_payload":"msg","entity_location":"data","override_data":"msg","x":330,"y":1060,"wires":[["97eda93485f2e9f9"],[]]},{"id":"2866e1abe9773b01","type":"server-state-changed","z":"1480002f.f9315","name":"l.room candle","server":"d9f720b4.8199a","version":4,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"light.spare_light_candle_ikea","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":true,"for":"0","forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"eventData"},{"property":"topic","propertyType":"msg","value":"","valueType":"triggerId"}],"x":90,"y":860,"wires":[["bc6a8b879a1eb5e3"]]},{"id":"f15282f26d598ebf","type":"server-state-changed","z":"1480002f.f9315","name":"bed south lamp","server":"d9f720b4.8199a","version":4,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"light.bedroom_south_light_lamp","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":true,"for":"0","forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"eventData"},{"property":"topic","propertyType":"msg","value":"","valueType":"triggerId"}],"x":80,"y":960,"wires":[["b3feb32bf366dd9a"]]},{"id":"b760f158f3b2c8bb","type":"server-state-changed","z":"1480002f.f9315","name":"kitchen ceiling","server":"d9f720b4.8199a","version":4,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"light.kitchen_light_ceiling","entityidfiltertype":"exact","outputinitially":false,"state_type":"str","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":true,"for":"0","forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"outputProperties":[{"property":"payload","propertyType":"msg","value":"","valueType":"entityState"},{"property":"data","propertyType":"msg","value":"","valueType":"eventData"},{"property":"topic","propertyType":"msg","value":"","valueType":"triggerId"}],"x":90,"y":1060,"wires":[["e0d2769bddf89db8"]]},{"id":"5b04d3e5b6c8b53b","type":"comment","z":"1480002f.f9315","name":"Circadian Rhythms","info":"","x":110,"y":720,"wires":[]},{"id":"a9af774de97831a1","type":"link out","z":"1480002f.f9315","name":"out - circadian","mode":"link","links":["d70654b12a5df196","1f2cbfdf4ae1549d","46c56bec0a744573"],"x":575,"y":760,"wires":[]},{"id":"d70654b12a5df196","type":"link in","z":"1480002f.f9315","name":"in - circadian","links":["a9af774de97831a1"],"x":215,"y":860,"wires":[["bc6a8b879a1eb5e3"]]},{"id":"1f2cbfdf4ae1549d","type":"link in","z":"1480002f.f9315","name":"in - circadian","links":["a9af774de97831a1"],"x":215,"y":960,"wires":[["b3feb32bf366dd9a"]]},{"id":"46c56bec0a744573","type":"link in","z":"1480002f.f9315","name":"in - circadian","links":["a9af774de97831a1"],"x":215,"y":1060,"wires":[["e0d2769bddf89db8"]]},{"id":"12a24aa25c3bdc34","type":"debug","z":"1480002f.f9315","name":"debug 6","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":940,"y":760,"wires":[]},{"id":"b86b91bc007b3a4b","type":"change","z":"1480002f.f9315","name":"flow.circadian","rules":[{"t":"set","p":"circadian","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":440,"y":760,"wires":[["a9af774de97831a1"]]},{"id":"21239eb49bee434b","type":"change","z":"1480002f.f9315","name":"msg.payload","rules":[{"t":"set","p":"payload","pt":"msg","to":"circadian","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":510,"y":860,"wires":[["39dab8ac3a091499"]]},{"id":"b5f70e55981de9fe","type":"change","z":"1480002f.f9315","name":"msg.payload","rules":[{"t":"set","p":"payload","pt":"msg","to":"circadian","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":510,"y":960,"wires":[["2ddd4f1f7b903168"]]},{"id":"97eda93485f2e9f9","type":"change","z":"1480002f.f9315","name":"msg.payload","rules":[{"t":"set","p":"payload","pt":"msg","to":"circadian","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":510,"y":1060,"wires":[["ace20c88462ce178"]]},{"id":"d9f720b4.8199a","type":"server","name":"Home Assistant","version":5,"addon":true,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true,"heartbeat":false,"heartbeatInterval":30,"areaSelector":"friendlyName","deviceSelector":"friendlyName","entitySelector":"friendlyName","statusSeparator":"at: ","statusYear":"hidden","statusMonth":"short","statusDay":"numeric","statusHourCycle":"h23","statusTimeFormat":"h:m","enableGlobalContextStore":true}]

claytonjn commented 1 year ago

There is no way to get brightness directly, but if you don't configure any switches but only add circadian_lighting: to configuration.yaml you will get a sensor that represents the approximate percentage of the current sun position as compared to its minimum/maximum for the day.

So for example, when the sensor value is 100 the sun is at the highest position, when it's 50 it's half-way between sunrise and sunset, when it's 0 it's at the horizon, and when it's -100 it's at its "lowest" position.

You should be able to use templates to turn that into brightness values based on your needs. The way Circadian Lighting handles it is if the sensor is greater than 0 (any time the sun is above the horizon) the brightness is 100%, if the sensor is negative, the brightness is equal to 100 minus the sensor value (technically plus because the sensor value is negative) - so brightness is 100% when the sun is at the horizon, dims down to 0 at the lowest position (although I set a minimum of 1%), then back up to 100%.

thargy commented 1 year ago

Thanks, that's perfect.

thargy commented 1 year ago

For anyone finding this question, here it is as a template:

{{ (((100 + states('sensor.circadian_values') | float, 1.0) | max), 100) | min }}

Although I was well aware of the sensor.circadian_values, I hadn't noticed that the primary value was as described above as I was 'blinded' by the attributes :man_facepalming: