aderusha / HASwitchPlate

LCD touchscreen for Home Automation
MIT License
732 stars 128 forks source link

Simplify HA automations for adjusting font/font size #17

Closed rohankapoorcom closed 6 years ago

rohankapoorcom commented 6 years ago

Hey @aderusha,

I noticed that most of the time when HA is updating the text for a specific object on the HASP, it has to recompute the font size and send that as well. I created a couple of scripts to:

This simplifies the number of operations that have to be done in an individual automation by half, improving readability and removing copying/pasting of values.

Here's an example:

Before:

    - service: mqtt.publish
      data:
        topic: 'hasp/plate01/command/p[2].b[7].font'
        payload_template: '{% if states.sensor.weather_current.state|length <= 6 -%}3{% elif (states.sensor.weather_current.state|length > 6) and (states.sensor.weather_current.state|length <= 10) %}2{% elif (states.sensor.weather_current.state|length > 10) and (states.sensor.weather_current.state|length <= 15) %}1{% else %}0{%- endif %}'

    - service: mqtt.publish
      data:
        topic: 'hasp/plate01/command/p[2].b[7].txt'
        payload_template: '"{{states.sensor.weather_current.state|wordwrap(20, wrapstring="\\r")}}"' 

After:

    - service: script.hasp_plate01_update_message
      data:
        objectId: 'p[2].b[7]'
        text: '"{{states.sensor.weather_current.state|wordwrap(20, wrapstring="\\r")}}"'
aderusha commented 6 years ago

HOLY COW this is amazing. I'm on the road this week which is going to result in a little more time required for me to test all of this but I FRICKEN LOVE IT and it simplifies a lot of things. I didn't know scripts worked this way! Thanks @rohankapoorcom, I'll push to get this all rolled into my test environment ASAP and then merge this fantastic work into the project.

rohankapoorcom commented 6 years ago

Let me know if you run into any problems, or if I missed any spots. I extracted and and generalized this functionality from my environment (which was tested) but (my environment) also contains many other changes related to this stuff.

rohankapoorcom commented 6 years ago

@aderusha I've discovered a few bugs with this current approach:

  1. First of all there are a few typos when I migrated things from my repo to this generic version
  2. HA doesn't allow scripts to get called in parallel so this approach leads to a lot of dropped messages being sent to the HASP and a mess of warning messages in the logs. The solution according to this post is a python script. I'll refactor with that and update this (hopefully tomorrow).