Read Data from text file and use as Trigger

Hi,

is it possible to read a text file and use the input as trigger.
The text file has one line that says: “Light1 on” and then the light goes on. And after like 10 Minutes the text file has a second line “Light1 off” and the light turns off. Something like that.

Take a look at the File Sensor.

I have added the sensor to my config and its working. But how do I put that into an automation?

The sensor now says Light 1 on - I need a trigger which executes when the sensor changes. So when another automation turns the light off he doesn’t read the sensor again and turns the light on again. I want to control about 3-4 things with this file so I guess in the automation has include some sort of if then.
But I don’t even know where to start with that.

What is your end goal with this? To me it sounds like you are over complicating things here.

Just use a basic trigger:

trigger:
  - platform: state
    entity_id: sensor.your_file_sensor

This triggers every time the sensor changes.

Yes, you are right. The trigger is not that much of a problem. This simple automation would work:

- id: File
  alias: File
  trigger:
    platform: state
    entity_id: sensor.file
  condition:
  - condition: state
    entity_id: sensor.file
    state: 'Light 1 off'
  action:
  - service: light.turn_off
    entity_id: light.light_1

BUT:

  1. I would prefer not to do one automation for every action.
  2. When I restart HA he does not trigger, which is good, but when the file changes with the same input it does not trigger too. What can I do about that?

I still don’t get what your end goal is and why you are doing it this way. Could you please elaborate?

1 Like

Yes, of course. I want to use my XMPP Server to communicate with HA. I send a message to a specific user and a text file with the message is created.
This way I can send a message like Heater on or Light 45 on to controll HA.

No clue about XMPP, can you not send commands to the Home Assistant Api directly or publish it to MQTT and pick up the command from there?

No, I was already happy to have this file to read from. And I don’t use MQTT. Just need some kind of if else action and another trigger when the file changes and it would be perfect.

I think i have found a way:

- id: File
  alias: File
  trigger:
    platform: state
    entity_id: sensor.file
  action:
  - service_template: >
      {% if is_state("sensor.file", "Switch 1 on") %}
        switch.turn_on
      {% elif is_state("sensor.file", "Switch 1 off") %}
        switch.turn_off
      {% endif %}
    entity_id: switch.switch_1
  - service_template: >
      {% if is_state("sensor.file", "Light 1 on") %}
        light.turn_on
      {% elif is_state("sensor.file", "Light 1 off") %}
        light.turn_off
      {% endif %}
    entity_id: light.light_1

This does work but since he runs both service templates Iam getting these errors in log:

ERROR (MainThread) [homeassistant.components.automation] Error while executing automation automation.file. Invalid data for call_service at pos 1: Service  does not match format <domain>.<name>

And I would still need another trigger when the file changes with the same input so it does trigger then but not on reboot.

Any help ist appreciated.

I agree you’re probably better off just having an automation per “event”. But if you must do it in one, then given the four “events” in your current example:

- id: File
  alias: File
  trigger:
  - platform: state
    entity_id: sensor.file
  action:
  - service_template: >
      {% if 'on' in trigger.to_state.state %}
        homeassistant.turn_on
      {% else %}
        homeassistant.turn_off
      {% endif %}
    data_template:
      entity_id: >
        {% set d,n = trigger.to_state.state.split()[0:2] %}
        {{ d|lower }}.{{ d|lower }}_{{ n }}
1 Like

Do you mind explaining this code to me, what does it do? :blush:
Does this prevent a trigger wenn HA restarts? Or does it prevent the error logs? Or Both? What does the block under entity_id mean? Do I have to change something when I add more “events”?

I think you need to read more of the documentation. You’re asking very basic questions.

Reading is one thing, understanding another. English is not my native language and sometimes I simply do not understand what things do. Trial and Error is how iam using HA most of the time…

Then it sounds like maybe you’re better off keeping things simple, which in this case, would mean writing an automation for each event with a simple state trigger, e.g.:

trigger:
- platform: state
  entity_id: sensor.file
  to: 'Switch 1 on'
action:
- service: switch.turn_on
  entity_id: switch.switch_1

But, to explain…

The service_template determines if you’re trying to turn something on or off, then calls the appropriate homeassistant service. Note that these services can turn any type of entity on or off, so the service_template should suffice if you’re always sending a command containing on or off (and as long as the string on is not anywhere else in the “event.”)

data_template allows the use of templates to specify the values for other parameters, such as entity_id. In this case it’s splitting the “event” string up (using the default delimiter of white space.) Then it’s taking the first and second pieces. The first specifies the entity’s domain, and the second is the number. Then it uses those to “build” the corresponding entity_id value.

Thanks for the explanation. And Iam sorry but I still don’t get what your code above does.^^ I have added it and do not see any difference. Maybe my comments have been misleading.

I just want the errors in logs to disappear and that it triggers again if the file changes with the same Input. Even with the simple state trigger I would need the last point.

Please don’t be angry with me. :hugs:

After some more testing (and some sleep :smiley: ) I think I now understand what your script should do. Sadly it does not do it.
With nonsense inside the file I get something like that: Unable to find service asdf/turn_on. But with a input that should work like “switch 1 on” he does nothing and nothing appears in log. The other thing is, not everything that I want to control has a number.

So if you don’t have enough of my stupidity, and I won’t blame you if thats the case, I would be happy for some help :slight_smile:

If you want a suggestion that will handle all the cases properly, you’ll have to define what all those cases are. I can’t guess what you might do. But even so, it seems you’re asking for someone to do the work for you, instead of trying to learn and do the majority of the work yourself. Again, if building a complicated automation is more than you can take on at this point in your learning curve, then maybe you should build more simplistic automations as opposed to fewer more complicated ones.

Thats the solution I have build today, I might add other actions in the future though:

- id: File
  alias: File
  trigger:
    platform: state
    entity_id: sensor.file
  action:
  - service_template: >
      {% if is_state("sensor.file", "Automatic on") %}
        automation.turn_on
      {% elif is_state("sensor.file", "Automatic off") %}
        automation.turn_off
      {% else %}
        script.dummy
      {% endif %}
    entity_id: automation.automatic
  - service_template: >
      {% if is_state("sensor.file", "Heater on") %}
        switch.turn_on
      {% elif is_state("sensor.file", "Heater off") %}
        switch.turn_off
      {% else %}
        script.dummy2
      {% endif %}
    entity_id: switch.heater
  - service_template: >
      {% if is_state("sensor.file", "Switch on") %}
        switch.turn_on
      {% elif is_state("sensor.file", "Switch off") %}
        switch.turn_off
      {% else %}
        script.dummy3
      {% endif %}
    entity_id: switch.switch_1

The Script is:

dummy:
  alias: 'Dummy script'
  sequence:
    delay: 00:00:00

I had to do 3 dummy scripts because otherwise Iam getting a “Script already running” error.
Outside of HA a script is running which will overwrite the content of the file once a day so I don’t have the problem with the same Input. I’ll have to see if once a day is enough.^^

As you can see I wasn’t all lazy and hoping for a solution from you. :stuck_out_tongue_winking_eye:
The solution works, but I guess there are more clever ways to do it. But thats over my capabilities. :slightly_smiling_face: