I have a mental block on how to deal with this logic… I have a single MQTT topic on which I’d like 2 controls. I’d like to have
a On/Off switch with no rules
a switch with automation which will turn it off again in 1 hour.
So here’s what I put together, but is of course not working since both switches work off the same MQTT topic. Toggling one switch will always also toggle the other and as a result the automation always runs, turning it off 1 hour later.
Ah good idea. Although that’s assuming the switch can then interpret a payload instead of just a flag. right? I have plans for a few different switches doing this sort of thing and I’m not sure if that’s possible with them.
a switch with automation which will turn it off again in 1 hour.
Ie, I want to have the option to turn something on and off manually without any automation, or choose to switch it on and automatically turn off after an hour. In this case, turn my generator on without restriction or turn it on for 1 hour only.
I wouldn’t think it to be such an “odd workaround”, though would welcome a better way to do it. My logic was that I would create 2 switches. One without automation and one with automation. But the problem is that they were both hooked up to 1 MQTT topic.
So eventually I realised the second virtual switch could work off a separate topic so that the virtual switch can control the actual switch.
Here’s the Switch definition… Maybe HA has a better way to define a virtual switch?
Here’s the automation hooked up to the virtual switch which will then control the actual switch. The actual switch can be just an ordinary MQTT switch or whatever. Nothing special.
# Turn on the Geni for 1 hour then turn off again
# When switched on, switch off after 1 hour
- alias: 'Rule - Turn Geni on'
trigger:
platform: state
entity_id: switch.generator_1hr
to: 'on'
action:
service: switch.turn_on
entity_id: switch.generator
# When virtual switch turns off, turn the geni off too
- alias: 'Rule - Turn Geni off'
trigger:
platform: state
entity_id: switch.generator_1hr
to: 'off'
action:
service: switch.turn_off
entity_id: switch.generator
# When virtual switch turns on, turn the geni on too
- alias: 'Rule - Once turned on, turn off again 1 Hour later'
trigger:
platform: state
entity_id: switch.generator_1hr
to: 'on'
for:
hours: 1
action:
service: switch.turn_off
entity_id: switch.generator_1hr
This is how it looks on the UI:
“Generator 1Hr” controls “Generator” and “Generator” is just an ordinary switch.