Somehow I get mixed messages in Node-RED from Event-state node which is a HA issue. I’ve got ESP8266 with some wall switches running ESPHome. binary_sensors publish JSON to a MQTT-topic
When I start listening to the topic in HA (Developer Tools > MQTT) or watch in MQTT Explorer no double messages show up . So that’s great. It’s a HA-thing.
S: 0 - Retain: false
Message 14 received on home/bedroom/switch_esph_double_left_bedroom at 9:38 PM:
{
"state": "ON",
"action": "right_hold",
"count": 3
}
QoS: 0 - Retain: false
Message 13 received on home/bedroom/switch_esph_double_left_bedroom at 9:38 PM:
{
"state": "ON",
"action": "right_hold",
"count": 2
}
QoS: 0 - Retain: false
Message 12 received on home/bedroom/switch_esph_double_left_bedroom at 9:38 PM:
{
"state": "ON",
"action": "right_hold",
"count": 1
}
Configuration sensor in HA;
- platform: mqtt
name: "switch_esph_double_left_bedroom"
unique_id: "espsw1"
icon: "mdi:toggle-switch"
force_update: true
state_topic: "home/bedroom/switch_esph_double_left_bedroom"
value_template: "{{ value_json.action }}"
availability_topic: "home/bedroom/switch_esph_double_left_bedroom/status"
payload_available: "online"
payload_not_available: "offline"
json_attributes_topic: "home/bedroom/switch_esph_double_left_bedroom"
According to HA documentation;
The state and the attributes of the sensor by design do not update in a synchronous manner if they share the same MQTT topic. Temporal mismatches between the state and the attribute data may occur if both the state and the attributes are changed simultaneously by the same MQTT message. An automation that triggers on any state change of the sensor will also trigger both on the change of the state or a change of the attributes. Such automations will be triggered twice if both the state and the attributes change. Please use a MQTT trigger and process the JSON in the automation directly via the
{{ trigger.payload_json }}
trigger data for automations that must synchronously handle multiple JSON values within the same MQTT message.
triggered more then once;
The docs state I should use MQTT Trigger and process in automation. What don’t understand what to trigger cause I use Node-RED for automations. HA is only my state-machine.
I’m fiddling with MQTT-trigger in automation but it doesn’t work at all. automation seems to be triggered by topic but action is nada. Also tried without condition. This is beyond my HA skills
automation;
- alias: esph_bedroom_json_attributes
initial_state: 'on'
trigger:
platform: mqtt
topic: home/bedroom/switch_esph_double_left_bedroom
condition:
condition: template
value_template: "{{ trigger.payload_json.count is defined }}"
action:
- service: sensor.set_value
data_template:
entity_id: "sensor.switch_esph_double_left_bedroom"
count: "{{ trigger.payload_json.count }}"
How can I fix this issue? Any pointers would be great!