D1mini, Tasmota and Deep Sleep per MQTT/rule?

Dear community,

I am playing around with a D1mini, Tasmota and Deep sleep mode. It all works fine, but battery life is 4days max. The sensor sends temperature & humidity values every 5min to home assistant. During the day, this is okay, but during night, I could easily life with a deep sleep time of 30min.
Is it possible to configure some rule(s) on the Tasmota itself or could I change the deep sleep Periode with a home assistant automation and MQTT?

Any ideas are highly appreciated.

Chris

I do that with scripts and a automation in HA. The automation selects the correct script based on time of day. I also have a automation to disable which sends an alert so I can login to the D1 make changes. It sleeps 10hrs overnight, 1hr early morning, late evening, and 10mins during the day.
Its solar/battery powered on my roof. I probably need to add a bigger solar panel or sleep more in winter to cover low solar days.

MQTT Sensor with timestamp

  - platform: mqtt
    name: "Pool Roof Duration"
    state_topic: "stat/ds1820/RESULT"
    value_template: '{{value_json.DeepSleepTime | timestamp_custom("%H:%M", False) }}'
    icon: "mdi:clock"
    unit_of_measurement: "HRs"       

Enable

- id: '1632830583124'
  alias: Enable DeepSleepTime ds1820
  description: Enable deep sleep script timing automation
  trigger:
  - platform: mqtt
    topic: tele/ds1820/LWT
    payload: Online
    id: 1 enable deep sleep
  condition: []
  action:
  - service: mqtt.publish
    data:
      topic: cmnd/ds1820/DeepSleepTime
  - wait_for_trigger:
    - platform: mqtt
      topic: stat/ds1820/RESULT
    continue_on_timeout: false
  - delay:
      hours: 0
      minutes: 0
      seconds: 30
      milliseconds: 0
  - choose:
    - conditions:
      - condition: and
        conditions:
        - condition: time
          after: '21:00'
          before: '23:30'
        - condition: not
          conditions:
          - condition: state
            entity_id: sensor.pool_roof_duration
            state: '10.00'
      sequence:
      - service: script.enable_deepsleeptime_ds1820_36000
    - conditions:
      - condition: and
        conditions:
        - condition: time
          after: 01:30
          before: 09:00
        - condition: not
          conditions:
          - condition: state
            entity_id: sensor.pool_roof_duration
            state: 01:00
      sequence:
      - service: script.enable_deepsleeptime_ds1820_3600
    - conditions:
      - condition: and
        conditions:
        - condition: time
          after: 09:00
          before: '17:00'
        - condition: not
          conditions:
          - condition: state
            entity_id: sensor.pool_roof_duration
            state: 00:10
      sequence:
      - service: script.enable_deepsleeptime_ds1820_600
    - conditions:
      - condition: and
        conditions:
        - condition: time
          after: '17:00'
          before: '21:00'
        - condition: not
          conditions:
          - condition: state
            entity_id: sensor.pool_roof_duration
            state: 01:00
      sequence:
      - service: script.enable_deepsleeptime_ds1820_3600
    default: []
  mode: single

scripts

enable_deepsleeptime_ds1820_600:
  alias: Enable DeepSleepTIme DS1820 600
  sequence:
  - service: mqtt.publish
    data:
      topic: cmnd/ds1820/DeepSleepTime
      payload: '600'
  mode: single
  icon: hass:sleep

enable_deepsleeptime_ds1820_3600:
  alias: enable DeepSleepTime DS1820 3600
  sequence:
  - service: mqtt.publish
    data:
      topic: cmnd/ds1820/DeepSleepTime
      payload: '3600'
  mode: single
  icon: hass:sleep

enable_deepsleeptime_ds1820_36000:
  alias: enable DeepSleepTime DS1820 36000
  sequence:
  - service: mqtt.publish
    data:
      topic: cmnd/ds1820/DeepSleepTime
      payload: '36000'
  mode: single
  icon: hass:sleep

Disable

- id: '1618226191887'
  alias: Disable DeepSleepTime ds1820
  description: ''
  trigger:
  - platform: mqtt
    topic: tele/ds1820/LWT
    payload: Online
  condition: []
  action:
  - service: automation.turn_off
    target:
      entity_id: automation.enable_deepsleeptime_ds1820
  - service: mqtt.publish
    data:
      topic: cmnd/ds1820/DeepSleepTime
      payload: '0'
  - wait_for_trigger:
    - platform: mqtt
      topic: stat/ds1820/RESULT
      payload: '{"DeepSleepTime":0}'
  - service: notify.mobile_app_alp_l29
    data:
      message: ds1820 is online and deep sleep is disabled for service
      title: deep sleep disabled
  - delay:
      hours: 0
      minutes: 0
      seconds: 30
      milliseconds: 0
  - service: automation.turn_off
    target:
      entity_id: automation.disable_deepsleeptime_ds1820
    data: {}
  mode: single

1 Like

@HasQT
wow, thank you very much! Due to covid and rebuilding our house, I did not yet had time to get my head into your config. I hope, I will soon find the time to do so.
I am already using automations for managing fans in our basement based on due point calculations, but I did not yet work with scripts. Do you have a good starting point to get familiar with scripts?

Chris

1 Like

Umm, I’m certainly no script God. In HA they are basically creating a permanent command you call again or repeat easily. Just follow the UI creation and look for examples on the forums if you get stuck. No doubt someone would be able to help if you posted some info you need help with.

I had to update a sensor since I originally posted this, just adding since I probably should done it a few weeks back
This bit here was to solve an error from the link below, needed to add the default bit to one of the other sensors that hit a similar topic.

The updated sensor

- platform: mqtt
    name: "Pool Roof Deeptime"
    state_topic: "stat/ds1820/RESULT"
    value_template: >
       {% set deepsleep = value_json.DeepSleep | default(none) %}
       {{ deepsleep.Epoch | timestamp_custom('%H:%M:%S %d/%m/%Y') if deepsleep else none }}
    icon: "mdi:clock"

Hi, I finally got the time to check your scripts, automation and the mqtt-helper-sensor. The scripts are very easy, the automation is elegant, but I struggle with your helper sensor. I would expect to get the DeepSleepTime from it. So, it should show the time duration, the device is going to sleep.
But when I use your template, it gives me a timestamp. (-> date & time of the next wakeup of the device)

Is this intended? If yes, I don’t understand the conditions in your automation :slight_smile:

EDIT: did you define a device_class?

Ok, I your sensor “Pool Roof Deeptime” seems to be a timestamp.
In your automation, you have the condition:

This condition checks for the state “10.00”. How does this work with a timestamp?

That’s the time stamp sensor. They are two separate entities. DeepSleep and DeepSleepTime.

The custom timestamp %H %M converts it to hours or minutes to give 10hrs 1hrs 10 mins for the conditions.

The sensors are as is, no device class set anywhere else.

You could also not include the custom timestamp and change the conditions to match the standard output.

Ah, sorry for not paying attention: I did not realize that there are two different mqtt-template-sensors.

I do not yet see what you need the “Pool Roof Deeptime” for, as it is not referenced in your automation/scripts. Perhaps when I start setting up thnigs, I will get it. :slight_smile:

Anyway, thank you very much for your inputs. It seems to be much easier, then I first thought.

1 Like

After I understood, what @HasQT did, I implemented the thing myself. Here my vesion, which is marginally different. All credits to @HasQT !

  • My script to disable deep sleep does not switch off the automation. I handle this within the automation. (If DeepSleepTime is 00:00, then wait 10min and if still true, send a notification to my mobile phone. I then can use one of the other scripts to set DeepSleepTime or keep on working on the device :slight_smile: .)
  • My automation to manage DeepSleepTime follows the same logik, but uses different times.
  • My MQTT-sensor does not have a unit_of_measurement, but uses a device_class.

MQTT Sensor to hold the currently set DeepSleepTime

### D1mini Wohnzimmer: DeepSleepTime ###
  - platform: mqtt
    name: "Wohnzimmer DeepSleepTime"
    unique_id: "mqtt.Wohnzimmer.DeepSleepTime"
    state_topic: "stat/D1mini_Wohnzimmer/RESULT"
    value_template: '{{value_json.DeepSleepTime | timestamp_custom("%H:%M", False) }}'
    device_class: "duration"

Automation to manage DeepSleepTime

alias: 'Wohnzimmer D1mini: DeepSleepTime Steuerung'
description: |-
  Steuerung der DeepSleepTime, um deb Akkuverbrauch reduzieren
  Zu Zeiten, in denen wir eher lĂĽften: 5min
  tagsĂĽber: 15min
  nachts: 60min
trigger:
  - platform: mqtt
    topic: tele/D1mini_Wohnzimmer/LWT
    payload: Online
condition: []
action:
  - service: mqtt.publish
    data:
      topic: cmnd/D1mini_Wohnzimmer/DeepSleepTime
  - wait_for_trigger:
      - platform: mqtt
        topic: stat/D1mini_Wohnzimmer/RESULT
    continue_on_timeout: false
  - delay:
      hours: 0
      minutes: 0
      seconds: 1
      milliseconds: 0
  - choose:
      - conditions:
          - condition: or
            conditions:
              - condition: time
                after: '06:00:00'
                before: '09:00:00'
              - condition: time
                after: '11:00:00'
                before: '14:00:00'
              - condition: time
                after: '17:00:00'
                before: '21:00:00'
          - condition: not
            conditions:
              - condition: state
                entity_id: sensor.wohnzimmer_deepsleeptime
                state: '00:05'
        sequence:
          - service: script.wohnzimmer_d1mini_deepsleep_5min
            data: {}
      - conditions:
          - condition: or
            conditions:
              - condition: time
                after: '09:00:00'
                before: '11:00:00'
              - condition: time
                after: '14:00:00'
                before: '17:00:00'
          - condition: not
            conditions:
              - condition: state
                entity_id: sensor.wohnzimmer_deepsleeptime
                state: '00:15'
        sequence:
          - service: script.1653076935556
            data: {}
      - conditions:
          - condition: or
            conditions:
              - condition: time
                after: '21:00:00'
                before: '06:00:00'
          - condition: not
            conditions:
              - condition: state
                entity_id: sensor.wohnzimmer_deepsleeptime
                state: '01:00'
        sequence:
          - service: script.1653076986433
            data: {}
      - conditions:
          - condition: state
            entity_id: sensor.wohnzimmer_deepsleeptime
            state: '00:00'
        sequence:
          - delay:
              hours: 0
              minutes: 10
              seconds: 0
              milliseconds: 0
          - condition: state
            entity_id: sensor.wohnzimmer_deepsleeptime
            state: '00:00'
          - service: notify.mobile_app_nokia_7_plus
            data:
              message: D1mini Wohnzimmer DeepSleepTime aktivieren.
              title: DeepSleepTime aktivieren
    default:
      - stop: D1mini Wohnzimmer DeepSleepTime nicht geändert.
mode: single

Scripts to set different DeepSleepTime on the D1mini.

Set DeepSleepTime to 300s (-> 5min)

alias: 'Wohnzimmer D1mini: DeepSleepTime 05min'
sequence:
  - service: mqtt.publish
    data:
      topic: cmnd/D1mini_Wohnzimmer/DeepSleepTime
      payload: '300'
mode: single
icon: mdi:sleep

Set DeepSleepTime to 900s (-> 15min)

alias: 'Wohnzimmer D1mini: DeepSleepTime 15min'
sequence:
  - service: mqtt.publish
    data:
      topic: cmnd/D1mini_Wohnzimmer/DeepSleepTime
      payload: '900'
mode: single
icon: mdi:sleep

Set DeepSleepTime to 3600s (-> 60min)

alias: 'Wohnzimmer D1mini: DeepSleepTime 60min'
sequence:
  - service: mqtt.publish
    data:
      topic: cmnd/D1mini_Wohnzimmer/DeepSleepTime
      payload: '3600'
mode: single
icon: mdi:sleep

Switch DeepSleep off to enable access to device

alias: 'Wohnzimmer D1mini: DeepSleepTime aus'
sequence:
  - wait_for_trigger:
      - platform: mqtt
        topic: tele/D1mini_Wohnzimmer/LWT
        payload: Online
  - service: mqtt.publish
    data:
      topic: cmnd/D1mini_Wohnzimmer/DeepSleepTime
      payload: '0'
  - wait_for_trigger:
      - platform: mqtt
        topic: stat/D1mini_Wohnzimmer/RESULT
        payload: '{"DeepSleepTime":0}'
  - service: notify.mobile_app_nokia_7_plus
    data:
      title: D1mini Wohnzimmer DeepSleep aus
      message: Der DeepSleep Modus des D1mini Wohnzimmer ist abgeschaltet.
mode: single
icon: mdi:toggle-switch-off-outline

One thing I did not yet understand: two of my three scripts are referenced with an ID. Where does the ID come from? I did not define the ID within the script… anyway, it works!

1 Like

If you make a script using the gui interface it’s gets allocated an Id.

I’m looking for similar solution.
I have gas meter far away with no electricity there. My gas boiler has a smart plug with power reading so based on this I could send MQTT messages to sleeping Wemos. I tried zigbee but it is not reliable.
What I want to achieve:

When boiler ON then send MQTT message to wemos to start counting impulses (but maybe just counting and sending counter state back every hour to save energy)
When boiler OFF then send MQTT message to wemos to deep sleep.

You can’t wake up a deep sleep device with mqtt. It would need to be awake to receive the message. You can do it with a wire… But doesn’t really help you(well perhaps the boiler could control that actually)

You can enable/disable DeepSleep and adjust the DeepSleepTime intervals, while the device is awake.

When the boiler is going you could send a message to disable DeepSleep, the esp8266/esp32 would need to be waking up to act on that message though.

Big enough battery/solar panel setup could get you a decent runtime…
I have 4 18650s and a small solar panel on my esp8266 roof temp sensor. It only wakes up every 10mins and sleeps at night though.

Esp32 have better low power management I believe.

https://tasmota.github.io/docs/DeepSleep/

In this case I need some kind of “wake up by GPIO” option. Se when reed switch detects change, wake up device and send MQTT about this change. When there is no change on GPIO for lets say 3 minutes go back to deep sleep. The problem is that I need device with antenna and Wemos D1 mini pro is perfect for this.
What would you suggest?

Perhaps LoRa is more suitable, AndreasSpiess does lots of low power stuff.

I’ve seen it. Not sure how to understand it. Is it some kind of work around to use gpio for wake up?

The circuit diagram?
The earlier tasmota deepsleep link explains it better than I can.

You can use that blue wire and a switch to disable/enable deepsleep. Although that still doesn’t wake up the device, it just prevents the next scheduled sleep after it wakes up.

Select another GPIO (let’s call it “GPIOn”) and connect it GND. This can be performed through a switch per the schematic below. Flipping the switch to “ON” will prevent Tasmota to enter DeepSleep again after next wake-up until the switch is flipped back OFF. On the diagram, blue denotes additional parts and connections to be able to disableDeepSleep. GPIOn should be defined as DeepSleep (182) in the configuration as shown below:

This sensor did not work anymore, because Tasmota sends two messages over the same toppic. This new versions filters on the payload and only updates, if there is a “DeepSleepTime”. If not, the current value will not be changed.

    - name: "Wohnzimmer DeepSleepTime"
      unique_id: "mqtt.Wohnzimmer.DeepSleepTime"
      state_topic: "stat/D1mini/Wohnzimmer/RESULT"
      value_template: >
        {{ value_json.DeepSleepTime | timestamp_custom("%H:%M", False, "unavailable") if value_json.DeepSleepTime is defined else this.state }}
      device_class: "duration"
      unit_of_measurement: "min"

The visualisation of the above is not good. New version with full credit to @123.

    - name: "Wohnzimmer DeepSleepTime"
      unique_id: "mqtt.Wohnzimmer.DeepSleepTime"
      state_topic: "stat/D1mini/Wohnzimmer/RESULT"
      value_template: >
        {% if value_json.DeepSleepTime is defined %}
          {{ relative_time(now() - timedelta(seconds = value_json.DeepSleepTime  )) }}
        {% else %}
          {{ this.state }}
        {% endif %}

Because the new sensor provides a string, I had to change the automation as well.