Automation logic problem

Hi all,

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

  1. a On/Off switch with no rules
  2. 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.

Any idea how I could get around this?

Switch definition:

switch Generator:
  name: "Generator"
  platform: mqtt
  qos: 2
#  retain: true
  state_topic: "energy/generator/stat/POWER"
  command_topic: "energy/generator/cmnd/POWER"
  
switch Generator1Hour:
  name: "Generator 1Hr"
  platform: mqtt
  qos: 2
#  retain: true
  state_topic: "energy/generator/stat/POWER"
  command_topic: "energy/generator/cmnd/POWER"

Automation definition

- alias: 'Rule - Generator for 1 Hour'
  trigger:
    platform: state
    entity_id: switch.generator_1hr
    to: 'on'
    for:
      hours: 1
  action:
    service: switch.turn_off
    entity_id: switch.generator_1hr

Why not pass a payload with the message? That way when you receive it. You will know what entity the message is intended for.

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.

what’s the end goal here? Why two switches? Seems like this can be handled with automations instead of this odd work around that you’re doing.

1 Like

I think I got it…

Like i said in the OP:

  1. a On/Off switch with no rules
  2. 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?

switch Generator:
  name: "Generator"
  platform: mqtt
  qos: 2
  state_topic: "energy/generator/stat/POWER"
  command_topic: "energy/generator/cmnd/POWER"
  
#  Virtual switch for running Geni for 1 hour only
switch Generator1Hour:
  name: "Generator 1Hr"
  platform: mqtt
  qos: 2
  state_topic: "energy/generator/cmnd/POWERAuto"
  command_topic: "energy/generator/cmnd/POWERAuto"

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:
image

“Generator 1Hr” controls “Generator” and “Generator” is just an ordinary switch.