Bitte um Code-Review: Twilight Farbtemperatursteuerung

Begonnen von Nielsiwilsi, 08 September 2019, 21:47:58

Vorheriges Thema - Nächstes Thema

Nielsiwilsi

Hallo Zusammen,

ich habe mir eine Automatik zusammenprogrammiert um bei Sonnenuntergang, bzw. -aufgang die Farbtemperatur an der Philips Hue LED Deckenleuchte zu steuern. Vorteil dabei ist, dass zigbee2mqtt über JSON auch r,g,b Werte für die IKEA Leuchten annimmt, die als nächstes dran sind.

Irgendwie hab ich nur das Gefühl, da ein Konstrukt gebaut zu haben, dass nicht wirklich optimal ist. Evtl gibts ja einfachere oder elegantere Lösungen zur Steuerung der Hue.

Klar, ich hab ein Twilight:
defmod Twilight Twilight 49.1234567 13.1234567 1 123456
attr Twilight room Wetter


Als nächstes hab ich die Hue mittels zigbee2mqtt in FHEM als MQTT Device angelegt.
defmod EG_Wohnzimmer_Lampe_Decke_Sofa MQTT2_DEVICE mqtt2client
attr EG_Wohnzimmer_Lampe_Decke_Sofa IODev mqtt2_client
attr EG_Wohnzimmer_Lampe_Decke_Sofa autocreate 0
attr EG_Wohnzimmer_Lampe_Decke_Sofa event-on-change-reading .*
attr EG_Wohnzimmer_Lampe_Decke_Sofa group Lichtsteuerung
attr EG_Wohnzimmer_Lampe_Decke_Sofa readingList zigbee2mqtt/EG_Wohnzimmer_Lampe_Decke_Sofa:.* { json2nameValue($EVENT, '', $JSONMAP) }\
zigbee2mqtt/EG_Wohnzimmer_Lampe_Decke_Sofa/set:.* x_mqttcom
attr EG_Wohnzimmer_Lampe_Decke_Sofa room EG_Wohnzimmer,MQTT2_DEVICE
attr EG_Wohnzimmer_Lampe_Decke_Sofa setList on:noArg zigbee2mqtt/EG_Wohnzimmer_Lampe_Decke_Sofa/set {"state":"$EVTPART0"}\
off:noArg zigbee2mqtt/EG_Wohnzimmer_Lampe_Decke_Sofa/set {"state":"$EVTPART0"}\
color_temp:slider,153,1,454 zigbee2mqtt/EG_Wohnzimmer_Lampe_Decke_Sofa/set {"state":"on","$EVTPART0":"$EVTPART1"}\
brightness:colorpicker,BRI,2,5,254 zigbee2mqtt/EG_Wohnzimmer_Lampe_Decke_Sofa/set {"state":"on","$EVTPART0":"$EVTPART1"}\
x_mqttcom zigbee2mqtt/EG_Wohnzimmer_Lampe_Decke_Sofa/set $EVTPART1
attr EG_Wohnzimmer_Lampe_Decke_Sofa stateFormat state


Nun Habe ich ein Dummy angelegt, um ein JSON zu speichern, dass dann auf das set-Topic published werden kann.
defmod EG_Wohnzimmer_Lampe_Decke_Sofa_JSON dummy
attr EG_Wohnzimmer_Lampe_Decke_Sofa_JSON event-on-change-reading .*
attr EG_Wohnzimmer_Lampe_Decke_Sofa_JSON group Lichtsteuerung
attr EG_Wohnzimmer_Lampe_Decke_Sofa_JSON readingList onoff brightness color_temp JSON
attr EG_Wohnzimmer_Lampe_Decke_Sofa_JSON room EG_Wohnzimmer
attr EG_Wohnzimmer_Lampe_Decke_Sofa_JSON setList onoff brightness:colorpicker,BRI,2,5,254 color_temp:slider,153,1,454 JSON
attr EG_Wohnzimmer_Lampe_Decke_Sofa_JSON stateFormat JSON


Das JSON wird mittels Notify erstellt.
defmod EG_Wohnzimmer_Lampe_Decke_Sofa_JSON_Create notify EG_Wohnzimmer_Lampe_Decke_Sofa_JSON:..* set EG_Wohnzimmer_Lampe_Decke_Sofa_JSON JSON {"state":"[EG_Wohnzimmer_Lampe_Decke_Sofa_JSON:onoff]","brightness":"[EG_Wohnzimmer_Lampe_Decke_Sofa_JSON:brightness]","color_temp":"[EG_Wohnzimmer_Lampe_Decke_Sofa_JSON:color_temp]"}
attr EG_Wohnzimmer_Lampe_Decke_Sofa_JSON_Create group Lichtsteuerung
attr EG_Wohnzimmer_Lampe_Decke_Sofa_JSON_Create room EG_Wohnzimmer


Und auch per Notify an das set-Topic übergeben.
defmod EG_Wohnzimmer_Lampe_Decke_Sofa_JSON_Publish notify EG_Wohnzimmer_Lampe_Decke_Sofa_JSON:JSON:.* set EG_Wohnzimmer_Lampe_Decke_Sofa x_mqttcom [EG_Wohnzimmer_Lampe_Decke_Sofa_JSON:JSON]
attr EG_Wohnzimmer_Lampe_Decke_Sofa_JSON_Publish group Lichtsteuerung
attr EG_Wohnzimmer_Lampe_Decke_Sofa_JSON_Publish room EG_Wohnzimmer


Color-Temp und Brightness werden über einen Faktor vom [Twilight:twilight_weather] Reading extrapoliert.
defmod EG_Wohnzimmer_Lampe_Decke_Sofa_JSON_TwilightBrightness notify Twilight:twilight_weather:..* set EG_Wohnzimmer_Lampe_Decke_Sofa_JSON brightness {(82+(ReadingsNum("Twilight","twilight_weather","")*1.7))}
attr EG_Wohnzimmer_Lampe_Decke_Sofa_JSON_TwilightBrightness group Lichtsteuerung
attr EG_Wohnzimmer_Lampe_Decke_Sofa_JSON_TwilightBrightness room EG_Wohnzimmer

und
defmod EG_Wohnzimmer_Lampe_Decke_Sofa_JSON_TwilightColorTemp notify Twilight:twilight_weather:..* set EG_Wohnzimmer_Lampe_Decke_Sofa_JSON color_temp {(400-(ReadingsNum("Twilight","twilight_weather","")*2))}
attr EG_Wohnzimmer_Lampe_Decke_Sofa_JSON_TwilightColorTemp group Lichtsteuerung
attr EG_Wohnzimmer_Lampe_Decke_Sofa_JSON_TwilightColorTemp room EG_Wohnzimmer


Wäre schön, wenn der Eine oder Andere mal drüber schauen könnte, wo Optimierungspotentiale liegen. Der Code selbst funktioniert ja erst mal.

Danke
Niels