Hi Guys,
I have multiple Rasberrys with HASSIO controlling relays and inputs as slaves. Slave Pi’s talk back to main Hassio via MQTT and all works well. BUT
The automation on each pi is very similar and long winded and would like to condense the similare automation routings into either one or two routines for a more elegent way of doing things.
Example. I have 16 relays each with 16 automations to send the MQTT back to the main instance.As these 16 seperate automations are the same but with relay number different it should be easy to template out the relay number and perform the automation in one hit. !!
As you can see its not the best way to automation and Im sure templating would be able to reduce the above down from 16 seperate automations into one.
Thanks for any ideas
Dave
A single automation can handle the on and off states of all 16 relays. The automation can be compact if you are willing to restructure the MQTT topics.
Change this format:
slave_2/relay_1
to this format:
slave_2/relay/1
The relay’s state topic will be:
slave_2/relay/1/state
If you do that then this single automation will handle all the relays:
Worked like a charm. Thank you so much. Realy appreciate your quick response.
I can change the MQTT topics easly to whatever, so theres no restrictions there. I have just tried to transpose your solution from the relays ( which works well ) to the Raspi Inputs.( binary_sensors ).Again 32inputs ,all done via the cluncky seperate automation for each.
- alias: input_03_on
trigger:
- platform: state
entity_id: binary_sensor.input_3
to: 'on'
action:
- service: mqtt.publish
data:
topic: 'slave_2/Input_3'
payload: 'on'
Now , im thinking this is going t be diffenent as I need to check each binary_senor.Input_XX to see which one has changed and pass that value on to the MQTT side ?
Again , sorry to ask but I suspect it would take you 30 seconds to figure out rather than my 30 days ( ok , perhaps 30 hours )
Thanks
@_DavidSheen
Please mark my post with the Solution tag. Only you, the author of the topic, can do this. It will automatically place a checkmark next to the topic’s title (indicating the topic has a solution) and place a link to the solution within your first post. All of this makes it easier for other users to find the solution.
HI 123 Taras, Almost worked. It does output MQTT on any one of the 16 inputs. but only outputs “input_xx” ( XX being 1 to 16 corresponding binary.sensor inputs). There is no additional payload attached to this payload “input_xx” such as ‘on’ or ‘off’
( By the way thanks for pointing me to MQTT explorer Loving that simple program. My new favourate ).
So the MQTT recieved on the master instance of HassIO is simple the topic of ‘slave_2/input_1’ and no payload. ??
Would I assume you would need to split the topic up.Similare to the topic on the ouput section.
But then again you cant do that as yourve already split the topic into “input_1”, so perhaps need to add another topic of on or off to the end…brain stating to hurt. I have an 80’s brain with the same 3.5k memory as I was born with and not be upgraded !!! as yet… Thanks Dave
You want that automation to publish the state of whichever binary_sensor triggered it, correct? If so, change the last line to:
payload: '{{trigger.to_state.state}}'
That represents the state the binary_sensor changed to. So if it was previously on and it changed to off, the payload’s value will be off.
Glad you like MQTT Explorer. I think the fact it automatically subscribes to all topics and then presents them in a treeview is what helps many people gain a better understanding of the broker’s operation.