Getting sensor data to trigger automation

Hi

I’m working on my Alarm clock for quite some while and while I managed to get all the information from my phone onto HA via tasker I don’t manage to create the automation.

I tried

- action:
  - data:
      entity_id: media_player.living_room_speaker
      message: alarm alarm alarm
    service: tts.google_say
  alias: '- Alarm Test'
  condition: []
  id: '1515744066606'
  trigger:
  - platform: template
    value_template: '{{ states.sensor.time.state == states.sensor.alarm_time.state }}'

The problem is that the trigger won’t work and I can’t figure out what I’m doing wrong. Did already couple of research online and testing without any result.

Thanks for your help

I’m no expert, and find the syntax challenging still, but it looks like your code is formatted and structured incorrectly. Here’s an example of one of mine:

- alias: "ecobee Good Morning mode"
  trigger:
    platform: time
    at: '06:48:00'
  condition:
    condition: state
    entity_id: 'binary_sensor.workday_sensor'
    state: 'on'
  action:
    service: climate.set_hold_mode
    data:
      entity_id: climate.thermostat
      hold_mode: smart4

My automation is triggered at a specific time, but only if it is a work day, and when triggered it sets a specific thermostat mode I’ve configured.

Alias is the top level in the syntax grouping the trigger, conditions and actions underneath.

I’m not sure what your condition is doing or how the google tts service fits in, but this format seems more correct over all to me:

- alias: Alarm Test
  trigger:
    platform: template
    value_template: '{{ states.sensor.time.state == states.sensor.alarm_time.state }}'
  condition: []
    id: '1515744066606'
  action:
    data:
      entity_id: media_player.living_room_speaker
      message: alarm alarm alarm
    service: tts.google_say

Hey singollo,

I was messing around with timers as well so here is my experience.
I wanted to have a timer that is configurable in the UI so I used input_datetime to control then “on” condition.
In addition I have a main switch to enable/disable the action via an input_boolean.
The trigger is fired every minute checking the conditions.
I had to use timestamp_custom() to make the internal HA time and the input_datetime compatible. So maybe that will do the trick for you as well.

- alias: 'Beleuchtung morgens an'
  trigger:
    platform: time
    minutes: '/1'
    seconds: 0
  condition:
    condition: and
    conditions:
      - condition: state
        entity_id: input_boolean.beleuchtung_automation
        state: 'on'
      - condition: template
        value_template: '{{ states.sensor.time.state == (states.input_datetime.beleuchtung_morgens_an.attributes.timestamp | int | timestamp_custom("%H:%M", False)) }}'
  action:
    - service: input_boolean.turn_on
    data:
      entity_id: input_boolean.beleuchtung_scene

In the end I have four of those automations for switching on and off at 2 different times each.

Thansk for all your help.

I actually made it work after I redid my original code from above. No idea why it didn’t work with the example aboce as i couldn’t find any difference in the end.

@VdkaShaker: the code from me was generated by the action tool from hass.io :slight_smile:

Still I can’t use it as is as I did a mistake in my thinking. I want basically that tasker sends the Alarmtime to HA. In ha i set up an automation for a fade in light that starts a certain time before. So i simulate the sunrise.

I try now to build the experience i had with the test to combine it with the instructions from this post.

Worked hard on it yesterday but haven’t quite made it work. I hope I’ll be able to post the final solution soon.

I managed to make the setup work as I reingeniertd the post mentioned above. Instead setting everything up in HA i used tasker for the Alarm time and if it should go off or not.

In my setup it looks like this
01

alarm time and status comes from tasker.

Tasker creates 3 different sensors. I also used the autoalarm plugin to get the hour and the minute of the Alarm set.

  1. loads the needed variables from autoalarm
  2. sets up the sensor for the hour
  3. sets up the sensor for the minutes
    4 and 5 basically set a sensor to either on or off for the status. if there is no alarm set within the next 24 hours it will switch the wakeup light to off.

6 to 8 is not important for this automation is just additional stuff i put into tasker.
6 and 7 give me an acustic feedback on my phone about if an alarm is set and when an alarm is set
8. activates an automation to switch everything off and is not

that you can see how i set up the sensors i made you screenshots of the settings as well.
you basically create a sensor with the http-post and then on Server:Port add https://yourdomain.duckdns.org:8123/api/states/sensor.name_of_sensor?api_password=your_HA_password

then in the data/file you add the Json data as you can see in my screenshots below. for a more detailed desciption of the process i recommend the following video: https://www.youtube.com/watch?v=CvSYZnKS0es

fist the status of the alarm and how i used if in tasker.

second the hour and the minute i used.

I did the following entried in the config file:

sensor:
  - platform: time_date
    display_options:
      - 'time'
  - platform: template
    sensors:
      wakeup_alarm_time:
        friendly_name: 'Alarm time'
        value_template: '{% if states.sensor.wakeup_hour.state|length == 3 %}0{% endif %}{{ states.sensor.wakeup_hour.state | int }}:{% if states.sensor.wakeup_minutes.state|length == 3 %}0{% endif %}{{ states.sensor.wakeup_minutes.state | int }}'
  - platform: template
    sensors:
      wakeup_start_time_lights:
        friendly_name: 'Fade-in start time'
        value_template: >
          {% if states.sensor.wakeup_alarm_time and states.input_number.wakeup_duration %}
          {% set alarmtime = states.sensor.wakeup_alarm_time.state %}
          {% set alarm_hour = alarmtime.split(":")[0] | int %}
          {% set alarm_min = alarmtime.split(":")[1] | int %}
          {% set light_dur = states.input_number.wakeup_duration.state | int %}
          {% set alarm_min_light = alarm_min - light_dur %}
          {% if alarm_min_light  < 0 %}
          {% set alarm_min_light = alarm_min_light + 60 %}
          {% set alarm_hour_light = alarm_hour - 1 %}
          {% if alarm_hour_light < 0 %}{% set alarm_hour_light = 23 %}{% endif %}
          {% if alarm_hour_light < 10 %}0{% endif %}{{ alarm_hour_light}}:{% if alarm_min_light < 10 %}0{% endif %}{{ alarm_min_light }}
          {% else %}
          {% if alarm_hour < 10 %}0{% endif %}{{ alarm_hour }}:{% if alarm_min_light < 10 %}0{% endif %}{{ alarm_min_light }}
          {% endif %}
          {% endif %}
  - platform: template
    sensors:
      wakeup_fadein_duration:
        friendly_name: 'Fade-in duration'
        value_template: '{{ states.input_number.wakeup_duration.state | int }}'
        unit_of_measurement: 'min'

input_number:
  wakeup_duration:
    name: Light fade-in duration
    icon: mdi:clock-in
    initial: 10
    min: 5
    max: 60
    step: 5

the automation looks like this:

- action:
  - data:
      entity_id: script.wakeup_bedroom
    service: script.turn_on
  alias: Alarm
  condition:
  - condition: state
    entity_id: sensor.alarm_status
    state: 'on'
  id: '1515744066606'
  trigger:
  - platform: template
    value_template: '{{ states.sensor.time.state == states.sensor.wakeup_start_time_lights.state
      }}'

With the script i’m not a 100% happy as I cant manage it like this in hassio environment but it works for now.

wakeup_bedroom:
  alias: Wakeup lighting, smooth transition
  sequence:
  - service: light.turn_on
    data_template:
      entity_id: light.bedroom
      brightness: 1
      xy_color:
      - 0.3698
      - 0.3723
  - delay:
      seconds: 1
  - service: light.turn_on
    data_template:
      entity_id: light.bedroom
      brightness: 255
      transition: '{{ states.sensor.wakeup_fadein_duration.state | multiply(60) |
        int }}'

I’m really fascinated by Home Assistant. I’m not a programmer at all and inspires me how I was able to set up such an automation within 3 weeks with the help of the community.