Skip to content

Instantly share code, notes, and snippets.

@bgautrea
Last active July 7, 2021 21:08
Show Gist options
  • Save bgautrea/6690ae31f4622764b45963227d2765f8 to your computer and use it in GitHub Desktop.
Save bgautrea/6690ae31f4622764b45963227d2765f8 to your computer and use it in GitHub Desktop.
blueprint:
name: "HASP p[x].b[y] displays the month + date with a smaller calendar icon"
description: |
# Description
A HASP button displays month + date on the right with a calendar icon on the left.
![Preview](https://github.com/aderusha/HASwitchPlate/raw/master/Documentation/Images/hasp_Display_Calendar_with_Icon.png?raw=true)
## HASP Page and Button reference
<details>
This automation is designed to work with the full-width buttons found on pages 1-3
| Pages 1-3 |
|-----------|
| ![Pages 1-3](https://github.com/aderusha/HASwitchPlate/raw/master/Documentation/Images/NextionUI_p1-p3_4buttons.png?raw=true) |
</details>
source_url: "https://github.com/aderusha/HASwitchPlate/blob/master/Home_Assistant/blueprints/automation/hasp_Display_Calendar_with_Icon.yaml"
domain: automation
input:
haspconnected:
name: "HASP Device"
description: "Select the HASP device"
selector:
entity:
integration: mqtt
domain: binary_sensor
device_class: connectivity
hasppage:
name: "HASP Page"
description: "Select the HASP page (1-3) for the calendar. Refer to the HASP Page and Button reference above."
default: 1
selector:
number:
min: 1
max: 3
mode: slider
haspbutton:
name: "HASP Button"
description: "Select the HASP button (4-7) for the calendar. Refer to the HASP Page and Button reference above."
default: 4
selector:
number:
min: 4
max: 7
mode: slider
mode: parallel
max_exceeded: silent
variables:
haspconnected: !input haspconnected
haspname: '{{ haspconnected.split(".")[1].split("_connected")[0] }}'
hasppage: !input hasppage
haspbutton: !input haspbutton
haspobject: '{{ "p[" ~ hasppage ~ "].b[" ~ haspbutton ~ "]" }}'
commandtopic: '{{ "hasp/" ~ haspname ~ "/command/" ~ haspobject }}'
jsontopic: '{{ "hasp/" ~ haspname ~ "/state/json" }}'
jsoncommandtopic: '{{ "hasp/" ~ haspname ~ "/command/json" }}'
selectedforegroundcolortopic: '{{ "hasp/" ~ haspname ~ "/light/selectedforegroundcolor/rgb" }}'
selectedbackgroundcolortopic: '{{ "hasp/" ~ haspname ~ "/light/selectedbackgroundcolor/rgb" }}'
unselectedforegroundcolortopic: '{{ "hasp/" ~ haspname ~ "/light/unselectedforegroundcolor/rgb" }}'
unselectedbackgroundcolortopic: '{{ "hasp/" ~ haspname ~ "/light/unselectedbackgroundcolor/rgb" }}'
activepage: '{% set activepage_entity = "number." ~ haspname ~ "_active_page" %}{% if states(activepage_entity)|lower == "none" %}-1{% else %}{{ states(activepage_entity) | int }}{% endif %}'
selectedfg: >-
{%- set colorsource="light." ~ haspname ~ "_selected_foreground_color" -%}
{%- set brightness = state_attr(colorsource, "brightness")|int / 255 -%}
{%- set red=(state_attr(colorsource, "rgb_color")[0] * brightness)|int -%}
{%- set green=(state_attr(colorsource, "rgb_color")[1] * brightness)|int -%}
{%- set blue=(state_attr(colorsource, "rgb_color")[2] * brightness)|int -%}
{{ (red|bitwise_and(248)*256) + (green|bitwise_and(252)*8) + (blue|bitwise_and(248)/8)|int }}
selectedbg: >-
{%- set colorsource="light." ~ haspname ~ "_selected_background_color" -%}
{%- set brightness = state_attr(colorsource, "brightness")|int / 255 -%}
{%- set red=(state_attr(colorsource, "rgb_color")[0] * brightness)|int -%}
{%- set green=(state_attr(colorsource, "rgb_color")[1] * brightness)|int -%}
{%- set blue=(state_attr(colorsource, "rgb_color")[2] * brightness)|int -%}
{{ (red|bitwise_and(248)*256) + (green|bitwise_and(252)*8) + (blue|bitwise_and(248)/8)|int }}
unselectedfg: >-
{%- set colorsource="light." ~ haspname ~ "_unselected_foreground_color" -%}
{%- set brightness = state_attr(colorsource, "brightness")|int / 255 -%}
{%- set red=(state_attr(colorsource, "rgb_color")[0] * brightness)|int -%}
{%- set green=(state_attr(colorsource, "rgb_color")[1] * brightness)|int -%}
{%- set blue=(state_attr(colorsource, "rgb_color")[2] * brightness)|int -%}
{{ (red|bitwise_and(248)*256) + (green|bitwise_and(252)*8) + (blue|bitwise_and(248)/8)|int }}
unselectedbg: >-
{%- set colorsource="light." ~ haspname ~ "_unselected_background_color" -%}
{%- set brightness = state_attr(colorsource, "brightness")|int / 255 -%}
{%- set red=(state_attr(colorsource, "rgb_color")[0] * brightness)|int -%}
{%- set green=(state_attr(colorsource, "rgb_color")[1] * brightness)|int -%}
{%- set blue=(state_attr(colorsource, "rgb_color")[2] * brightness)|int -%}
{{ (red|bitwise_and(248)*256) + (green|bitwise_and(252)*8) + (blue|bitwise_and(248)/8)|int }}
text: '{{(now().strftime("%b "))~now().day}}'
font: 9
ypos: "{{(haspbutton|int - 4) * 67 + 2}}" # calculate the top pixel position based on the selected button
xpos: 0
icon: ""
iconwidth: 50
iconheight: 55
iconfont: 8
trigger:
- platform: state
entity_id: !input haspconnected
to: "on"
- platform: homeassistant
event: start
- platform: time
at: "00:00:00"
- platform: mqtt
topic: "hasp/+/state/json"
- platform: mqtt
topic: "hasp/+/light/selectedforegroundcolor/rgb"
- platform: mqtt
topic: "hasp/+/light/selectedbackgroundcolor/rgb"
- platform: mqtt
topic: "hasp/+/light/unselectedforegroundcolor/rgb"
- platform: mqtt
topic: "hasp/+/light/unselectedbackgroundcolor/rgb"
condition:
- condition: state
entity_id: !input haspconnected
state: "on"
action:
- choose:
#########################################################################
# Apply text and style when "EXECUTE" is pressed by the user
- conditions:
- condition: template
value_template: "{{ trigger is not defined }}"
sequence:
- service: mqtt.publish
data:
topic: "{{jsoncommandtopic}}"
payload: >-
["{{haspobject}}.font={{font}}",
"{{haspobject}}.xcen=2",
"{{haspobject}}.ycen=1",
"{{haspobject}}.pco={{selectedfg}}",
"{{haspobject}}.bco={{selectedbg}}",
"{{haspobject}}.pco2={{unselectedfg}}",
"{{haspobject}}.bco2={{unselectedbg}}",
"{{haspobject}}.txt=\"{{text}} \""
{%- if activepage|int == hasppage|int -%}
,"delay=1","xstr {{xpos}},{{ypos}},{{iconwidth}},{{iconheight}},{{iconfont}},{{selectedfg}},0,1,1,3,\"{{icon}}\""
{%- endif -%}]
#########################################################################
# Update the calendar text every day at midnight. If the selected page is currently active, also place the icon.
- conditions: #
- condition: template
value_template: '{{ trigger.platform == "time" }}'
sequence:
- service: mqtt.publish
data:
topic: "{{jsoncommandtopic}}"
payload: >-
["{{haspobject}}.txt=\"{{text}} \""
{%- if activepage|int == hasppage|int -%}
,"delay=1","xstr {{xpos}},{{ypos}},{{iconwidth}},{{iconheight}},{{iconfont}},{{selectedfg}},0,1,1,3,\"{{icon}}\""
{%- endif -%}]
#########################################################################
# Catch MQTT events
- conditions:
- condition: template
value_template: '{{ trigger.platform == "mqtt" }}'
sequence:
- choose:
#########################################################################
# Catch incoming JSON messages
- conditions:
- condition: template
value_template: "{{ (trigger.topic == jsontopic) and trigger.payload_json is defined }}"
sequence:
- choose:
#########################################################################
# Icon overlay
- conditions: # Somebody pressed our button which hides the overlaid icon. Put it back.
- condition: template
value_template: '{{ (trigger.topic == jsontopic ) and (trigger.payload_json.event == haspobject ) and (trigger.payload_json.value == "OFF") }}'
sequence:
- service: mqtt.publish
data:
topic: "{{jsoncommandtopic}}"
payload: '["delay=1","xstr {{xpos}},{{ypos}},{{iconwidth}},{{iconheight}},{{iconfont}},{{selectedfg}},0,1,1,3,\"{{icon}}\""]'
- conditions: # Page changed to our page, so place the icon on the screen.
- condition: template
value_template: '{{ (trigger.topic == jsontopic ) and (trigger.payload_json.event == "page" ) and (trigger.payload_json.value == hasppage|int) }}'
sequence:
- service: mqtt.publish
data:
topic: "{{jsoncommandtopic}}"
payload: '["delay=1","xstr {{xpos}},{{ypos}},{{iconwidth}},{{iconheight}},{{iconfont}},{{selectedfg}},0,1,1,3,\"{{icon}}\""]'
#########################################################################
# Theme: Apply selected foreground color when it changes.
# Any change to the button will remove the overlaid icon.
- conditions:
- condition: template
value_template: "{{ trigger.topic == selectedforegroundcolortopic }}"
sequence:
- service: mqtt.publish
data:
topic: "{{commandtopic}}.pco"
payload: "{{trigger.payload}}"
- condition: template
value_template: "{{ activepage|int == hasppage|int }}"
- delay: "00:00:00.1"
- service: mqtt.publish
data:
topic: "{{jsoncommandtopic}}"
payload: '["delay=1","xstr {{xpos}},{{ypos}},{{iconwidth}},{{iconheight}},{{iconfont}},{{trigger.payload}},0,1,1,3,\"{{icon}}\""]'
#########################################################################
# Theme: Apply selected background color on change
- conditions:
- condition: template
value_template: "{{ trigger.topic == selectedbackgroundcolortopic }}"
sequence:
- service: mqtt.publish
data:
topic: "{{commandtopic}}.bco"
payload: "{{trigger.payload}}"
- delay: "00:00:00.1"
- service: mqtt.publish
data:
topic: "{{jsoncommandtopic}}"
payload: '["delay=1","xstr {{xpos}},{{ypos}},{{iconwidth}},{{iconheight}},{{iconfont}},{{selectedfg}},0,1,1,3,\"{{icon}}\""]'
#########################################################################
# Theme: Apply unselected foreground color on change
- conditions:
- condition: template
value_template: "{{ trigger.topic == unselectedforegroundcolortopic }}"
sequence:
- service: mqtt.publish
data:
topic: "{{commandtopic}}.pco2"
payload: "{{trigger.payload}}"
- delay: "00:00:00.1"
- service: mqtt.publish
data:
topic: "{{jsoncommandtopic}}"
payload: '["delay=1","xstr {{xpos}},{{ypos}},{{iconwidth}},{{iconheight}},{{iconfont}},{{selectedfg}},0,1,1,3,\"{{icon}}\""]'
#########################################################################
# Theme: Apply unselected background color on change
- conditions:
- condition: template
value_template: "{{ trigger.topic == unselectedbackgroundcolortopic }}"
sequence:
- service: mqtt.publish
data:
topic: "{{commandtopic}}.bco2"
payload: "{{trigger.payload}}"
- delay: "00:00:00.1"
- service: mqtt.publish
data:
topic: "{{jsoncommandtopic}}"
payload: '["delay=1","xstr {{xpos}},{{ypos}},{{iconwidth}},{{iconheight}},{{iconfont}},{{selectedfg}},0,1,1,3,\"{{icon}}\""]'
# Catch any other trigger conditions, apply styles, place text, and then place icon if our target page is currently active
default:
- service: mqtt.publish
data:
topic: "{{jsoncommandtopic}}"
payload: >-
["{{haspobject}}.font={{font}}",
"{{haspobject}}.xcen=2",
"{{haspobject}}.ycen=1",
"{{haspobject}}.pco={{selectedfg}}",
"{{haspobject}}.bco={{selectedbg}}",
"{{haspobject}}.pco2={{unselectedfg}}",
"{{haspobject}}.bco2={{unselectedbg}}",
"{{haspobject}}.txt=\"{{text}} \""
{%- if activepage|int == hasppage|int -%}
,"delay=1","xstr {{xpos}},{{ypos}},{{iconwidth}},{{iconheight}},{{iconfont}},{{selectedfg}},0,1,1,3,\"{{icon}}\""
{%- endif -%}]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment