[Solved] Virtual switch to control 2 physical switches based on time of day

Hello,

I have 2 physical switches that I can control through HA.
I want to be able to control one or the other based on the time of the day using a 3rd virtual switch. This virtual switch should also mimic the state the physical switch is in.
Lets say for instance that the physical switches are called First and Second and the virtual one Virtual.
I want the Virtual switch to control the First switch from 10:00 to 13:00 and the Second switch otherwise (from 13:01 to 00:00 and from 00:00 to 09:59). It should also have exactly the same state as the switch it is controlling.

I tried a few things in HA but was not able to get the expected result.
What would be the best and easiest way to achieve this?

Thanks

create a template switch. info here:

on the turn_on and turn_off, check to see what time it is and turn on/off the appropriate phyical switch.

Thank you for your reply.
I was trying this in yaml, but I always seemed to get an error (homeassistant.exceptions.HomeAssistantError: Template rendered invalid entity IDs) : Performing actions - Home Assistant
After fiddling around with the Web UI, I managed to do what I wanted.

glad you got it to work. if you decide you want to figure out what was going on with your template approach, post your code and one of us can help identify the issue

@armedad
I went to helpers and created a switch template.
In the turn_on section I put the following, as explained here :

action: switch.turn_on
target:
  entity_id: >
    {% if 10 <= now().hour < 13 %}
      switch.first
    {% else %}
      switch.second
    {% endif %}

I can control switch.first and switch.second from HA without any problem, but when I switch this virtual switch, I get the following error in the logs :

homeassistant.exceptions.HomeAssistantError: Template rendered invalid entity IDs: {% if 10 <= now().hour < 13 %}
  switch.first
{% else %}
  switch.second
{% endif %}

Trying the same yaml code in → developer tools → template, outputs the correct switch based on the time in the if statement, so the code seems good.

Reporting this just to try to understand where is the mistake, as the solution I finally implemented is more “elegant” from my POV.

only the entity id is allowed to be templated

</s> <s>action: switch.turn_on</s> <s>target:</s> <s> entity_id: ></s> <s> switch.{{"first" if 10 <= now().hour < 13 else "second"}}</s> <s>

1 Like

So the documentation in the link I provided is wrong? Or is it not applicable here?

huh… well blimey! i had it cached in my head that you can’t do that. but that doc page clearly gives an example that is what you had.

and i just tested it on a quick test it worked just fine. i think i had it flipped in my head that action/services can’t have the whole thing templated, but only the second part… so sorry about getting mixed up on that.

so now i’m confused by the error you’re getting.

could you post your whole automation?

It is not an automation, it is a switch template in helpers. Not sure what to post.
You can create the switch template and in turn_on add the following :

action: switch.turn_on
target:
  entity_id: >
    {% if 10 <= now().hour < 13 %}
      switch.first
    {% else %}
      switch.second
    {% endif %}

Then simply turn on the switch and look at the logs.

well… playing around with it, it really looks like a bug to me in the helpers. if you put that same code in dev tools->actions you’ll see that it renders fine. and it fails in the helper template.

That is what I was reporting below (in my third post), but not being knowledgeable enough, I taught that I was just making a mistake.

All in all, it is possible to achieve the same result with the Web UI (no yaml code needed), so not a big deal.