t0mg / wordclock

ESP32 based DIY word clock project
Apache License 2.0
66 stars 11 forks source link

Home automation integration #35

Closed K-Brix closed 7 months ago

K-Brix commented 8 months ago

This is a feature request .

Is there a possibility to add either a REST api or a Mqtt client to your project. I would like to be able to change at least the color, the brightness from home assistant. Case use would be to switch off the leds when no one is in the room or dim when the tv is on etc. Also the color could be changed for instance when someone is ringing or the temperature is under 0… I checked the IotWebConf project and it seems that it can handle these features.

https://github.com/prampec/IotWebConf/blob/master/examples/IotWebConf06MqttApp/IotWebConf06MqttApp.ino

Is it something you would consider?

t0mg commented 8 months ago

Yes mqtt is planned, stay tuned :)

K-Brix commented 8 months ago

Yes mqtt is planned, stay tuned :)

Nice. Can’t wait. Very cool to see this repo getting some features and polishing.

t0mg commented 8 months ago

there's now a minimalist implementation where you can configure mqtt from the web ui and then just publish to "color/set" with a hexa color value. it's still wip but feedback would be welcome

t0mg commented 8 months ago

@K-Brix "color/set" works nicely from my testing.

Since you have concrete plans for it, do you have any suggestions of features to implement for the mqtt client?

Is there a point in publishing a state topic? What kind of info?

Anything relevant to listen to beyond color setting ? Brightness is a bit annoying because it would conflict with the LDR sensor. Setting a darker color from HA would not conflict with that and achieve the same result.

Should the color set via mqtt be saved to eeprom like the web UI does, or should a reboot restore last saved web UI value? (I'm leaning towards not persisting it).

Should we prefix the channel with the device name in case there are multiple ? Or make it configurable in the web UI maybe ?

K-Brix commented 8 months ago

I haven’t had the time to test it yet cause you’re coding too fast. Regarding what feature to expose to Mqtt: I would like it to be automatically set up as a light in HA. This can be achieved by following these guidelines, either via Matt topics or via a single json payload (https://www.home-assistant.io/integrations/light.mqtt/) For my use case, I would especially like to be able to turn off the ws leds altogether when no one is at home or at night, or by using a presence sensor. Of course you can expose all the config in mqtt (langage, am/pm, …) and other informations like uptime, wifi strength, ambiant luminosity (could be used by other devices) I think it’s OK to revert to the last saved color on reboot. But could be a user defined setting The Chanel should be clearly identified either with the device name or a random sequence (wordclock_AEF5). Better yet if it’s user configurable but with a default to a unique id. Thanks for your work.

K-Brix commented 8 months ago

Ok, I compiled yesterday's code using plateformio (first time I tried it) and enabling loging to serial. I tested it on a breadboard with no led plugged so I can confirm that this is working only from the logs feedback.

I can connect to my mqtt server with no trouble but wordclock doesn't publish any topic. I was able to publish to color/set and got confirmation from the esp32 serial console:

Incoming: color/set - #f242f5
Updating color
Brightness has changed, updating
Updating display

However, the color change is not reflected in the webui wich still display the eeprom value:

|-- [display_group]
|   |-- 'boot_animation_enabled' with value: '1'
|   |-- 'clockface_language' with value: '2'
|   |-- 'show_ampm' with value: '0'
|   |-- 'ldr_sensitivity' with value: '0'
|   \-- 'color' with value: '#4be12d'
K-Brix commented 8 months ago

Compiled again with last commit f21b039 I can now succesfully subscribe to wordclock/status/ldr and publish to wordclock/color/set

t0mg commented 8 months ago

Thank you for testing! Do the topic names make sense to you? Any suggestion on how often we should publish the ldr value?

K-Brix commented 8 months ago

Yes the topics names make sense as is. If you're open to suggestion, I'd propose

[clockname]/sensor/ldr -> current ldr reading
[clockname]/light/color -> current color
[clockname]/light/color/set -> publish hex color
[clockname]/light/switch -> on/off state of the light
[clockname]/light/switch/set (don't know if it's possible or if the color should be set to #000000 instead)
And maybe some user settings (/set)
[clockname]/config/language
[clockname]/config/ntp
[clockname]/config/timezone
[clockname]/config/ldrsensitivity

... the more the better :)

Regarding the ldr value update frequency, I'd say once every 15 seconds or so should be enough. It would be confusing to add a webui config for such a niche feature. Maybe the update frequency could be in a #define somewhere in the code so that a user with specific needs could modify it before compiling

Thanks again

DaveDavenport commented 8 months ago

Is it better to only publish on change, but with persistence flag set?

t0mg commented 8 months ago

What is a persistence flag? Another option could be to request some info and the clock could publish when it gets a request

K-Brix commented 8 months ago

That is called a retain flag. Should be implemented in the mqtt library cause it’s used a lot in the protocol. Tells the server to retain the value until it’s changed. I would agree it’s best practice to publish on change but the ldr reading must change quite often.

t0mg commented 7 months ago

Ok I see, in that case we should not publish the raw ldr measure as we were doing here. But surely this should work nicely for other types of information like current color.

t0mg commented 7 months ago

Actually I'll keep it simple for now and just publish every 15s. K-Brix is right, changes happen more often than that. I can still set the retain flag. Same with color and all the rest actually.

t0mg commented 7 months ago

Ok I followed your recommendation. As of commit 281a693 you have

Sensor now publishes every 15s. Note: I put every publish in "retain" I don't know if that's the way.

Here's an example configuration that works for me in HA:

mqtt:
  - light:
      name: "Wordclock"
      state_topic: "wordclock/light/switch"
      command_topic: "wordclock/light/switch/set"
      rgb_state_topic: "wordclock/light/color"
      rgb_command_topic: "wordclock/light/color/set"
      payload_off: "OFF"
      payload_on: "ON"
      qos: 0
      optimistic: false
  - sensor:
      name: "Wordclock light sensor"
      state_topic: "wordclock/sensor/ldr"

If we are happy with this I'll document the MQTT feature.

K-Brix commented 7 months ago

Ok sounds perfect to me. That's exactly what I needed and I will be testing it this weekend. Just a question. The LDR reading on the ADC pin is in volt, is it? there's a specific sensor type in esphome for this but I'm not sure I understand the point of it https://esphome.io/components/sensor/resistance

t0mg commented 7 months ago

Added an availability topic ("offline"/"online") in 7648734 so HA can mark the clock as unavailable when it's disconnected instead of retaining the last known state.

mqtt:
  - light:
      name: "Wordclock"
      availability_topic: "wordclock/availability"
      state_topic: "wordclock/light/switch"
      command_topic: "wordclock/light/switch/set"
      rgb_state_topic: "wordclock/light/color"
      rgb_command_topic: "wordclock/light/color/set"
      qos: 0
      optimistic: false
  - sensor:
      name: "Light sensor"
      availability_topic: "wordclock/availability"
      state_topic: "wordclock/sensor/ldr"

image

image

K-Brix commented 7 months ago

Ok @t0mg I compiled and tested the most recent source code and I can confirm the mqtt implementation is working as expected, including home assistant mqtt light and sensor integration. should'nt be difficult to use it in homekit as well through HA. Same for Jeedom or other home automation system. Very happy with the whole thing. I have one clock in my office, but I'm considering building another one for my home (maybe 60x60 or 90x90 cm, your svg's should scale nicely and a whole lot more room for the electronics).

t0mg commented 7 months ago

Thank you for your feedback! Lovely to hear that, all of it. Would love see that giant wordclock it you ever build it :)

Closing the ticket!

t0mg commented 6 months ago

@K-Brix just a heads up that I've just added the ability to display custom content on the led matrix - as home assistant user I thought that might be useful to show icons in some automations.

Details : https://github.com/t0mg/wordclock/blob/main/software/UsersManual.md#paint

K-Brix commented 6 months ago

Nice touch. I may have some ideas like special icons on family birthdays, an icon warning when the house uses too much electricity and the price is high....

t0mg commented 6 months ago

Thanks for the feedback, let me know if it works for you!